我想在使用iPhone SDK 3.0的線程上運行NSTimer。我認爲我正在做的一切正確(新的runloop等)。如果我呼籲viewDidDissappear [定時器無效]雖然我得到這個錯誤:在線程上運行NSTimer
bool _WebTryThreadLock(bool), 0x3986d60: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now... Program received signal: 「EXC_BAD_ACCESS」.
這裏是我的代碼:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[activityIndicator startAnimating];
NSThread* timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(timerStart) object:nil]; //Create a new thread
[timerThread start]; //start the thread
}
-(void)timerStart
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
//Fire timer every second to updated countdown and date/time
timer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(method) userInfo:nil repeats:YES] retain];
[runLoop run];
[pool release];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[timer invalidate];
}
當我刪除計時器一切無效線路工作正常。我不應該使其失效或我犯了一些其他錯誤?
感謝
好感謝您的幫助 – John 2010-02-04 17:07:21