-(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