Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何等待调用委托的按钮_Objective C_Xcode4.5 - Fatal编程技术网

Objective c 如何等待调用委托的按钮

Objective c 如何等待调用委托的按钮,objective-c,xcode4.5,Objective C,Xcode4.5,我想创建一个函数来显示UIActionSheet,等待用户按下按钮,然后返回按下的按钮 @interface UIActionSheetHandler () <UIActionSheetDelegate> @end @implementation UIActionSheetHandler -(NSInteger) buttonIndexWithMessage:(NSString *) title andArrayOfOptions:(NSArray *) options {

我想创建一个函数来显示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
@接口UIActionSheetHandler()
@结束
@实现UIActionSheetHandler
-(NSInteger)按钮带有消息的索引:(NSString*)标题和数组选项:(NSArray*)选项
{
UIActionSheet*actionSheet=[[UIActionSheet alloc]initWithTitle:title
代表:赛尔夫
取消按钮:无
破坏性按钮:无
其他按钮:无];
用于(选项中的NSString*strOption){
[actionSheet AddButton,标题为:strOption];
}
[actionSheet showInView:[BGMDApplicationsPointers窗口]];
//等待调用委托。
返回0;//我想在这里返回buttonIndex。
}
-(无效)操作表:(UIActionSheet*)操作表单击按钮索引:(NSInteger)按钮索引
{
//我应该把它放在这里什么
}
@结束

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

我想你误解了代理的概念。不要返回从
ButtonIndex中按下的按钮,消息为:andArrayFoptions:

事实上,UIActionSheet在那个时候甚至不可见

按下按钮后,UIActionSheet将调用代理的
actionSheet:ClickedButtonIndex:
方法

那么,你把它放在哪里//我应该把它放在这里那里你被交给了被按下按钮的索引。在那里,你可以对按下的按钮做出相应的反应。 e、 g:

这个有效

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

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

您为什么要这样做?当按下按钮时,按钮索引将被传递,因为我已经有了等待按钮索引的功能。在这种风格上编程更容易。如果我不同意,请注意:)…我很清楚代表的概念。我只是想用不同的方式。然后继续,让我们知道结果。您可能希望实现自己的警报视图。我已尝试使用您的上述代码。但我的主线程因等待委托函数而永久停止。我的委托函数用于GCDAsyncSocket。
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    self.buttonIndex=buttonIndex;
    self.operation.suspended=false;
}