这是使用委托删除对象的正确方法吗?

时间:2012-02-29 15:40:19

标签: ios delegates

“测试”调用[委托onSignal:nil],然后我们将杀死“test”对象。 “test”对象没有到达onSignal函数的末尾。(它不好吗?)

@implementation FirstViewController
@synthesize array;

- (void)viewDidLoad
{ 
 [super viewDidLoad];

 self.array = [[[NSMutableArray alloc] initWithCapacity:20] autorelease];  

 Test *test = [[Test alloc] init];
 test.delegate = self;    
 [array addObject:test];
 [test release];        
}

-(void) onSignal:(id)sender
{
 [array removeObjectAtIndex:0];
}

和测试

@protocol TestDelegate <NSObject>
-(void) onSignal:(id) sender;
@end

@interface Test : NSObject
@property (nonatomic, assign) id<TestDelegate> delegate;
-(void) signal:(id) sender;
@end

@implementation Test
@synthesize delegate;

- (id)init
{  
if (self = [super init]) 
{
    [self performSelector:@selector(signal:) withObject:nil afterDelay:5];
}

return self;
}

-(void) signal:(id) sender
{
 [delegate onSignal:nil];
}

0 个答案:

没有答案