0

I want to clear all objects which added to NSVIew before call any function. How can I do that?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
HTKT611
  • 161
  • 1
  • 1
  • 11

2 Answers2

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