I want to clear all objects which added to NSVIew before call any function. How can I do that?
Asked
Active
Viewed 1,747 times
0
-
What are you trying to accomplish? Are you creating the view programmatically or loading from a Nib/xib – uchuugaka Jul 23 '13 at 09:20
-
I create it programmatically. – HTKT611 Jul 24 '13 at 01:39
2 Answers
5
I use the following function
-(void)clearAllSubviewsOfView :(NSView *)parent
{
for (NSView *subview in [parent subviews]) {
[subview removeFromSuperview];
}
}

Lithu T.V
- 19,955
- 12
- 56
- 101
0
You can make something like this:
// TSClearSupporting.h
@protocol TSClearSupporting <NSObject>
- (void) clear;
@end
// TSTextField.h
#import <Cocoa/Cocoa.h>
#import "TSClearSupporting.h"
@interface TSTextField : NSTextField <TSClearSupporting>
@end
// TSTextField.m
#import "TSTextField.h"
@implementation TSTextField
- (void) clear
{
self.stringValue = @"";
}
@end
// TSMainView.m
#import "TSMainView.h"
#import "TSClearSupporting.h"
@implementation TSMainView
- (IBAction) clearAll: (id)sender
{
NSArray* subViews = self.subviews;
for (NSView* view in subViews)
{
if ([view conformsToProtocol: @protocol(TSClearSupporting)])
{
[view performSelector: @selector(clear)];
}
}
}

stosha
- 2,108
- 2
- 27
- 29