非常簡單的使用NSThread和NSTimer,如下所示。當用戶點擊一個按鈕時,我希望每秒打印@「aaaaa」,但@「aaaaa」只打印一次?似乎NSTimer不起作用...幫助NSTimer不能在NSThread中工作?
//this method is connected to 'run' button in IB
- (IBAction)begin1:(id)sender {
[NSThread detachNewThreadSelector:@selector(thread1) toTarget:self withObject:nil];
}
-(void)thread1{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(aaa)
userInfo:nil
repeats:YES];
[timer fire];
}
-(void)aaa{
NSLog(@"aaaaa");
}
謝謝。似乎我也錯過了創建NSTimer的'NSRunLoop'。 – 2012-03-30 03:15:42
他爲什麼需要保留它?他不使用它在另一個功能,它應該是可行的。或者你使用ARC? – Gargo 2012-03-30 04:42:59