objective-c - IOS:UIAlert 中的两个按钮控件

标签 objective-c xcode ios uialertview

我在 IBAction 中有这段代码:

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

    [alertView show];
    [alertView release];

现在,如果我按下“确定”,它必须做一件事,如果我按下“取消”,它必须做另一件事。但它必须在 IBAction 内部完成。

最佳答案

您需要实现UiAlertViewDelegate 方法。

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

因此委托(delegate)函数应该如下所示。

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

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

          }
  }

关于objective-c - IOS:UIAlert 中的两个按钮控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6200191/

相关文章:

ios - 在 Cordova 项目中没有收到解析推送通知

ios - 如何让 Google-Maps API 正常工作?

iphone - 多行标签文本对齐

iphone - 从 UIViewController 调用 UIApplicationDelegate 方法

ios - 以编程方式将段落标题添加到 UITextView

ios - children count 总是返回0 ,无法正确到达

ios - 如何按值过滤NSDictionary并从中创建新的NSDictionary?

ios - IBOutlet 未在 SWIFT 中连接

objective-c - VLCKit : VLCMediaPlayerDelegate in a Cocoa Application

ios - 为什么我不能用 Fishhook Hook 我的私有(private)函数