【问题标题】:UIAlertView Button ActionUIAlertView 按钮操作
【发布时间】:2011-04-23 10:08:20
【问题描述】:

如何对UIButton click 使用两个操作?我有一个 UIAlertView 显示两个按钮。再次播放并退出。现在我想在这些按钮的单击事件中执行两个方法。

【问题讨论】:

  • 请创建更多上下文,您的问题包含 UIAlertView 这个词,并且描述没有暗示它是如何适应的。
  • 我已经更新了问题。请立即检查。告诉我该怎么做?
  • 我无法解决问题是的...你能帮帮我吗?

标签: ios objective-c cocoa-touch


【解决方案1】:

更新 - 2016 年 5 月

UIAlertView 已弃用。您现在可以按照here 的说明使用 UIAlertController。

UIAlertView 的旧答案

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

【讨论】:

  • 我应该把这个功能放在哪里: - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { //取消点击...做你的action } else if (buttonIndex == 1) { //reset clicked } }
  • 一定要遵守 UIAlertViewDelegate 协议。
  • 哎呀是的..也做一件事,在你的控制器的.h文件中确认UIAlertViewProtocol,即当我们声明@interface SomeController:UIViewController
  • 我的 .h 文件已经像这样开始了:我如何在这里使用另一个委托?@interface GameTableView : UITableViewController
  • [alertView cancelButtonIndex]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多