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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:41:49  来源:igfitidea点击:

UIAlertView Button Action

iosobjective-ccocoa-touch

提问by sohel14_cse_ju

How can I use two actions for UIButtonclick?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 的旧答案

  1. 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];
    
  2. To handle AlertView button click, you have to conform toUIAlertViewDelegateprotocol.

    @interface YourViewController:UIViewController<UIAlertViewDelegate>{
      .......
      .......
    }
    
  3. Then implementUIAlertViewDelegateprotocol methods,

    - (void)alertView:(UIAlertView *)alertView 
                       clickedButtonAtIndex:(NSInteger)buttonIndex{
        if (buttonIndex == [alertView cancelButtonIndex]){
          //cancel clicked ...do your action
        }else{
          //reset clicked
        }
    }
    
  1. 你可以像这样创建一个 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];
    
  2. 要处理 AlertView 按钮单击,您必须 符合UIAlertViewDelegate协议。

    @interface YourViewController:UIViewController<UIAlertViewDelegate>{
      .......
      .......
    }
    
  3. 然后实现UIAlertViewDelegate协议方法,

    - (void)alertView:(UIAlertView *)alertView 
                       clickedButtonAtIndex:(NSInteger)buttonIndex{
        if (buttonIndex == [alertView cancelButtonIndex]){
          //cancel clicked ...do your action
        }else{
          //reset clicked
        }
    }