-(NSInteger) buttonIndexWithMessage:(NSString *) title andArrayOfOptions:(NSArray *) options
{
self.operation=[NSOperationQueue new];
[self.operation addOperationWithBlock:^{
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (NSString * strOption in options) {
[actionSheet addButtonWithTitle:strOption];
}
[actionSheet showInView:[BGMDApplicationsPointers window]];
}];
self.operation.suspended=true; //Okay make t
}];
[self.operation waitUntilAllOperationsAreFinished];//Don't get out of the function till user act.
//Wait till delegate is called.
return self.buttonIndex;//I want to return buttonIndex here.
}
執行點不斷移動,直到返回self.buttonIndex,即使self.operation尚未完成。爲什麼waitUntilAllOperationsAreFinished不會在此代碼中等待?
我想用函數樣式來轉換委託樣式調用。所以我想創建一個顯示UIActionSheet並返回索引用戶選擇的函數。 – 2013-03-12 14:16:28
如何暫停一個線程呢? – 2013-03-12 14:17:04
你在找什麼是線程同步。您的後臺線程需要阻塞,直到在主線程上檢測到按鈕按下爲止。請參閱https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html。 – fishinear 2013-03-12 14:32:12