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
IOS: two button control in a UIAlert
提问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 UiAlertViewDelegate
method.
你需要实现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 IBAction
method. Having an IBAction method block and wait for a response from the UIAlertView is the wrong way to work in CocoaTouch. The IBAction
should 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 IBAction
methods 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 IBAction
when not using an UIAlertView
, and place your current code in the IBAction
after this chain.
将链IBAction->UIAlertView->alertView:clickedButtonAtIndex:
视为IBAction
不使用 的时候UIAlertView
,并将您当前的代码放在IBAction
此链的后面。