0

我想在调用任何函数之前清除所有添加到 NSVIew 的对象。我怎样才能做到这一点?

4

2 回答 2

5

我使用以下功能

-(void)clearAllSubviewsOfView :(NSView *)parent
{
    for (NSView *subview in [parent subviews]) {
        [subview removeFromSuperview];
    }
}
于 2013-07-23T09:21:51.507 回答
0

你可以做这样的事情:

// 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)];
        }
    }
}
于 2013-07-23T09:04:52.893 回答