我真的很苦惱與多線程程序,當外部變量更改時更改IBOutlet。使用多線程訪問UIcomponents
帶班頭:
@interface QuestionViewController : UIViewController{
// IBOutlet
IBOutlet UIBarButtonItem *changeButton;
...
}
啓動線程:
// Starts new thread with method 'statusPollMethod' to determine when status
[NSThread detachNewThreadSelector:@selector(statusPollMethod:) toTarget:self withObject: @"Voting"];
開始:
-(void)statusPollMethod:requiredStatus{
GetSessionDataApi *statusPoll = [GetSessionDataApi new];
SessionCache *tempSessionCache = [SessionCache new];
...
@synchronized(self){
// Infinite Loop
while(1) {
// If sessionStatus is 'Voting' set button as displayed
if ([[tempSessionCache sessionStatus] isEqualToString: (@"Voting")]){
NSLog(@"Status is Voting!");
// Ungreys and sets the button
[changeButton setTitle:@"Submit"];
changeButton.enabled = YES;
}
// Wait for around 3 seconds
[NSThread sleepForTimeInterval:3];
}
}
return;
}
的問題是,第二個線程只是偶爾完成對IBOutlet中的任何行動。 IBOutlet也被第一個線程訪問,所以我知道我可能有一些多線程問題,我不確定如何保護IBOutlet免受此影響。
建議我已經瀏覽過Apple Docs/Overflow帖子,但我仍然感到困惑。
我真的很感謝你的幫忙!
Tim
是你的問題是不執行下面的LOC - [changeButton setTitle:@「Submit」]; changeButton.enabled = YES; – rishi 2012-02-13 16:51:04