【问题标题】:Adding a simple UIAlertView添加一个简单的 UIAlertView
【发布时间】:2011-05-26 17:10:18
【问题描述】:

我可以使用哪些入门代码来制作一个带有一个“确定”按钮的简单 UIAlertView?

【问题讨论】:

  • 是否要等到单击“确定”按钮后再执行操作?
  • @sudo rm -rf :不,我只需要它说“Dee dee doo doo”之类的。无需任何操作。

标签: ios objective-c iphone cocoa-touch uialertview


【解决方案1】:
UIAlertView *alert = [[UIAlertView alloc]
 initWithTitle:@"Title" 
 message:@"Message" 
 delegate:nil //or self
 cancelButtonTitle:@"OK"
 otherButtonTitles:nil];

 [alert show];
 [alert autorelease];

【讨论】:

    【解决方案2】:

    当您希望显示警报时,请执行以下操作:

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ROFL" 
                                                        message:@"Dee dee doo doo." 
                                                        delegate:self 
                                                        cancelButtonTitle:@"OK" 
                                                        otherButtonTitles:nil];
    [alert show];
    
        // If you're not using ARC, you will need to release the alert view.
        // [alert release];
    

    如果您想在单击按钮时执行某些操作,请实现此委托方法:

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
        // the user clicked OK
        if (buttonIndex == 0) {
            // do something here...
        }
    }
    

    并确保您的委托符合UIAlertViewDelegate 协议:

    @interface YourViewController : UIViewController <UIAlertViewDelegate> 
    

    【讨论】:

    • 如果您有超过 1 个警报视图,您可以使用标签来确定谁呼叫了代理。
    【解决方案3】:

    作为前两个答案(用户“sudo rm -rf”和“Evan Mulawski”)的补充,如果您在单击警报视图时不想做任何事情,您可以只分配、显示和释放它。您不必声明委托协议。

    【讨论】:

      【解决方案4】:
      UIAlertView *myAlert = [[UIAlertView alloc] 
                               initWithTitle:@"Title"
                               message:@"Message"
                               delegate:self
                               cancelButtonTitle:@"Cancel"
                               otherButtonTitles:@"Ok",nil];
      [myAlert show];
      

      【讨论】:

        【解决方案5】:

        这是一个完整的方法,只有一个按钮,一个“确定”,用于关闭 UIAlert:

        - (void) myAlert: (NSString*)errorMessage
        {
            UIAlertView *myAlert = [[UIAlertView alloc]
                                  initWithTitle:errorMessage
                                  message:@""
                                  delegate:self
                                  cancelButtonTitle:nil
                                  otherButtonTitles:@"ok", nil];
            myAlert.cancelButtonIndex = -1;
            [myAlert setTag:1000];
            [myAlert show];
        }
        

        【讨论】:

          【解决方案6】:

          其他答案已经提供了 iOS 7 和更早版本的信息,但是 UIAlertView 在 iOS 8 中已弃用

          在 iOS 8+ 中,您应该使用 UIAlertController。它是UIAlertViewUIActionSheet 的替代品。文档:UIAlertController Class Reference。还有一篇关于NSHipster 的好文章。

          要创建一个简单的警报视图,您可以执行以下操作:

          UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title"
                                                                                   message:@"Message"
                                                                            preferredStyle:UIAlertControllerStyleAlert];
          //We add buttons to the alert controller by creating UIAlertActions:
          UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
                                                             style:UIAlertActionStyleDefault
                                                           handler:nil]; //You can use a block here to handle a press on this button
          [alertController addAction:actionOk];
          [self presentViewController:alertController animated:YES completion:nil];
          

          Swift 3 / 4 / 5:

          let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
          //We add buttons to the alert controller by creating UIAlertActions:
          let actionOk = UIAlertAction(title: "OK",
              style: .default,
              handler: nil) //You can use a block here to handle a press on this button
          
          alertController.addAction(actionOk)
          
          self.present(alertController, animated: true, completion: nil)
          

          请注意,由于它是在 iOS 8 中添加的,因此此代码不适用于 iOS 7 及更早版本。所以,遗憾的是,现在我们必须像这样使用版本检查:

          NSString *alertTitle = @"Title";
          NSString *alertMessage = @"Message";
          NSString *alertOkButtonText = @"Ok";
          
          if (@available(iOS 8, *)) {
              UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle
                                                                  message:alertMessage
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                        otherButtonTitles:alertOkButtonText, nil];
              [alertView show];
          }
          else {
              UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle
                                                                                       message:alertMessage
                                                                                preferredStyle:UIAlertControllerStyleAlert];
              //We add buttons to the alert controller by creating UIAlertActions:
              UIAlertAction *actionOk = [UIAlertAction actionWithTitle:alertOkButtonText
                                                                 style:UIAlertActionStyleDefault
                                                               handler:nil]; //You can use a block here to handle a press on this button
              [alertController addAction:actionOk];
              [self presentViewController:alertController animated:YES completion:nil];
          }
          

          Swift 3 / 4 / 5:

          let alertTitle = "Title"
          let alertMessage = "Message"
          let alertOkButtonText = "Ok"
          
          if #available(iOS 8, *) {
              let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
              //We add buttons to the alert controller by creating UIAlertActions:
              let actionOk = UIAlertAction(title: alertOkButtonText,
                  style: .default,
                  handler: nil) //You can use a block here to handle a press on this button
          
              alertController.addAction(actionOk)
              self.present(alertController, animated: true, completion: nil)
          }
          else {
              let alertView = UIAlertView(title: alertTitle, message: alertMessage, delegate: nil, cancelButtonTitle: nil, otherButtonTitles: alertOkButtonText)
              alertView.show()
          }
          

          UPD:针对 Swift 5 进行了更新。在 Obj-C 中用可用性检查替换了过时的类存在检查。

          【讨论】:

          • 你不应该发布可以工作但不能工作的代码。不要使用 MyOwnUtilsClass,只需编写检查 ios 版本的代码。
          • @csharpwinphonexaml,我不同意。这将是代码的不必要的复杂化。当前版本说明了 UIAlerView/UIAlertController 的用法,而系统版本检查不是这个问题的主题。在 Swift 中,有一种内置的方法可以检查操作系统版本,所以我使用了它。 Objective-C 有几种方法,但没有一个是优雅的。
          • 我这么说是因为我知道不是每个人都擅长理解每一段代码,并且知道如何用有效的代码替换它。
          【解决方案7】:

          This page 展示了如何在使用 Swift 时添加 UIAlertController。

          【讨论】:

            【解决方案8】:

            UIAlertView 在 iOS 8 上已弃用。因此,要在 iOS 8 及更高版本上创建警报,建议使用 UIAlertController:

            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Alert Message" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
            
                // Enter code here
            }];
            [alert addAction:defaultAction];
            
            // Present action where needed
            [self presentViewController:alert animated:YES completion:nil];
            

            这就是我实现它的方式。

            【讨论】:

              【解决方案9】:

              带有数组数据的简单警报:

              NSString *name = [[YourArray objectAtIndex:indexPath.row ]valueForKey:@"Name"];
              
              NSString *msg = [[YourArray objectAtIndex:indexPath.row ]valueForKey:@"message"];
              
              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:name
                                                              message:msg
                                                             delegate:self
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles:nil];
              [alert show];
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2014-11-02
                • 1970-01-01
                • 2018-06-18
                • 2017-01-14
                • 1970-01-01
                • 1970-01-01
                • 2011-10-12
                • 1970-01-01
                相关资源
                最近更新 更多