xcode IOS:UIAlert 中的两个按钮控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6200191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:10:26  来源:igfitidea点击:

IOS: two button control in a UIAlert

objective-cxcodeiosuialertview

提问by cyclingIsBetter

I have this code in a IBAction:

我在 IBAction 中有这个代码:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"NO!" 
                                                        message:@"danger"
                                                       delegate:self 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:@"Annul", nil];

    [alertView show];
    [alertView release];

Now if I push "OK" it must do a thing and if I push "Annul" it must do another thing. But it must be done inside the IBAction.

现在,如果我按“OK”,它必须做一件事,如果我按“Annul”,它必须做另一件事。但是必须在IBAction里面完成。

回答by Jhaliya

You need to implement UiAlertViewDelegatemethod.

你需要实现UiAlertViewDelegate方法。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

So the delegate function should be like below.

所以委托函数应该如下所示。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  {
          if(buttonIndex == 0)//OK button pressed
          {

          }
          else if(buttonIndex == 1)//Annul button pressed.
          {

          }
  }

回答by Mr. Berna

Jhaliya's answer is great, except it doesn't address blackguardian's request to run this inside a IBActionmethod. Having an IBAction method block and wait for a response from the UIAlertView is the wrong way to work in CocoaTouch. The IBActionshould just present the UIAlertView. Then the delegate function in Jhaliya's answer should parse which way to proceed, "OK", or "Annul". This delegate function can then perform the action (possibly by calling further methods). CocoaTouch's event handling is not designed for IBActionmethods to block awaiting further user input.

Jhaliya 的回答很好,只是它没有解决 blackguardian 在IBAction方法中运行它的请求。使用 IBAction 方法阻塞并等待来自 UIAlertView 的响应是在 CocoaTouch 中工作的错误方式。本IBAction应该只是目前的UIAlertView中。然后 Jhaliya 的答案中的委托函数应该解析继续进行的方式,“OK”或“Annul”。然后,此委托函数可以执行操作(可能通过调用其他方法)。CocoaTouch 的事件处理不是为IBAction阻止等待进一步用户输入的方法而设计的。

Think of the chain IBAction->UIAlertView->alertView:clickedButtonAtIndex:as just the IBActionwhen not using an UIAlertView, and place your current code in the IBActionafter this chain.

将链IBAction->UIAlertView->alertView:clickedButtonAtIndex:视为IBAction不使用 的时候UIAlertView,并将您当前的代码放在IBAction此链的后面。