“测试”调用[委托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];
}