繁体   English   中英

如何清除可可NSView上的所有对象

[英]How to clear all object on NSView in Cocoa

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

我使用以下功能

-(void)clearAllSubviewsOfView :(NSView *)parent
{
    for (NSView *subview in [parent subviews]) {
        [subview removeFromSuperview];
    }
}

您可以进行如下操作:

// 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)];
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM