ios UIAlertView 按钮操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5763581/
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
UIAlertView Button Action
提问by sohel14_cse_ju
How can I use two actions for UIButton
click?I have a UIAlertView showing with two button.Play again and exit.Now i want to execute two method in the click event of these buttons.
如何使用两个操作进行UIButton
点击?我有一个 UIAlertView 显示两个按钮。再次播放并退出。现在我想在这些按钮的点击事件中执行两个方法。
回答by Krishnabhadra
UPDATE - May 2016
更新 - 2016 年 5 月
UIAlertView is deprecated. You can now use UIAlertController as explained here.
UIAlertView 已弃用。您现在可以按照此处的说明使用 UIAlertController 。
Old Answer with UIAlertView
UIAlertView 的旧答案
You can create a UIAlertView like this
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Really reset?" message:@"Do you really want to reset this game?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"reset", nil]; [alert show];
To handle AlertView button click, you have to conform to
UIAlertViewDelegate
protocol.@interface YourViewController:UIViewController<UIAlertViewDelegate>{ ....... ....... }
Then implement
UIAlertViewDelegate
protocol methods,- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == [alertView cancelButtonIndex]){ //cancel clicked ...do your action }else{ //reset clicked } }
你可以像这样创建一个 UIAlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Really reset?" message:@"Do you really want to reset this game?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"reset", nil]; [alert show];
要处理 AlertView 按钮单击,您必须 符合
UIAlertViewDelegate
协议。@interface YourViewController:UIViewController<UIAlertViewDelegate>{ ....... ....... }
然后实现
UIAlertViewDelegate
协议方法,- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == [alertView cancelButtonIndex]){ //cancel clicked ...do your action }else{ //reset clicked } }