1

我想创建一个显示 UIActionSheet 的函数,等到用户按下按钮然后返回按下的按钮

@interface UIActionSheetHandler () <UIActionSheetDelegate>

@end
@implementation UIActionSheetHandler


-(NSInteger) buttonIndexWithMessage:(NSString *) title andArrayOfOptions:(NSArray *) options
{

    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]];

    //Wait till delegate is called.
    return 0;//I want to return buttonIndex here.
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //What should I put it here
}
@end

我如何使//等待,直到调用委托来等待?哦,我不想让主线程等待,但我想我可以解决那个问题。

4

2 回答 2

2

我猜你误解了委托的概念。不能返回按下的按钮buttonIndexWithMessage:andArrayOfOptions:

事实上,UIActionSheet 在那个时间点甚至都不可见。

按下按钮后,UIActionSheet 将调用actionSheet:clickedButtonAtIndex:委托的方法。

因此,在您输入的位置,您将//What should I put it here there获得已按下按钮的索引。在那里,您可以对相应按下的按钮做出反应。例如:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog (@"Button pressed with index: %d", buttonIndex); 
}
于 2013-03-12T10:21:03.803 回答
1

这个有效

self.operation=[NSOperationQueue new]; //These four line is necessary to suspend the whole thread.
self.operation.suspended=true; //Okay make t
[self.operation addOperationWithBlock:^{}];
[self.operation waitUntilAllOperationsAreFinished];//Don't get out of the function till user act.

然后在委托

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    self.buttonIndex=buttonIndex;
    self.operation.suspended=false;
}

注意:我仍在寻找更优雅的解决方案。

于 2013-03-12T14:21:48.540 回答