【问题标题】:Alert view in iphoneiphone中的警报视图
【发布时间】:2023-03-11 19:07:01
【问题描述】:

我是 iPhone 应用程序开发的新手。我想设计一个带有 2 个按钮的警报视图:OKCancel。当用户触摸OK 按钮时,我将打印一条消息,上面写着hello。当他们触摸Cancel 按钮时,我将打印cancel

请帮忙;我该怎么做?

【问题讨论】:

标签: iphone objective-c cocoa-touch


【解决方案1】:

显示警报:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Do you want to say hello?"
                                                message:@"More info..."
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"Say Hello",nil];
[alert show];
[alert release];

响应任何被点击的按钮:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        NSLog(@"Cancel Tapped.");
    }
    else if (buttonIndex == 1) {
        NSLog(@"OK Tapped. Hello World!");
    }
}

有关详细信息,请参阅 UIAlertView Class ReferenceUIAlertView Delegate Protocol Reference

【讨论】:

  • 您的答案不应该还包括 .h 文件中 的必要性吗?我意识到你引用了类和委托协议,但很容易想象 OP 跳过了那部分:(
  • 最好使用“buttonIndex!=alertView.cancelButtonIndex”,以防您稍后删除取消按钮并忘记更改委托调用中的按钮索引。
  • Steve,您没有将“nil”作为您的 otherButtonTitles 列表的最后一项!因此,当复制和使用此代码时,它会在编译时发出警告,然后在运行时崩溃...
  • 现在,[alert release] 将无法编译。 :)
【解决方案2】:

由于选择的答案已被弃用,这里是新的解决方案:

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
                               message:@"This is an alert."
                               preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
   handler:^(UIAlertAction * action) {}];

[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];

iOs Developer guide所示。

【讨论】:

    【解决方案3】:

    使用以下 sn-p 显示警报

    UIAlertView *alert = [[UIAlertView 分配] initWithTitle:@"做出明智的选择" 消息:无 代表:自己 cancelButtonTitle:@"取消" otherButtonTitles:@"OK", nil]; [警报显示];

    delegate 设置为 self,因此当警报被解除时,我们自己的类将收到回调。委托必须实现 UIAlertViewDelegate 协议。

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger) buttonIndex{ if (buttonIndex == 1) { // 做吧! } 别的 { // 取消 } }

    【讨论】:

      【解决方案4】:

      对于目标 C

      UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"My Alert"
                  message:@"This is an action sheet." 
                  preferredStyle:UIAlertControllerStyleAlert]; // 1
          UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"one"
                  style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                      NSLog(@"You pressed button one");
                  }]; // 2
          UIAlertAction *secondAction = [UIAlertAction actionWithTitle:@"two"
                  style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                      NSLog(@"You pressed button two");
                  }]; // 3
      
          [alert addAction:firstAction]; // 4
          [alert addAction:secondAction]; // 5
      
          [self presentViewController:alert animated:YES completion:nil]; // 6
      

      对于斯威夫特:

      let alert = UIAlertController(title: "My Alert", message: "This is an action sheet.", preferredStyle: .Alert) // 1
          let firstAction = UIAlertAction(title: "one", style: .Default) { (alert: UIAlertAction!) -> Void in
              NSLog("You pressed button one")
          } // 2
      
          let secondAction = UIAlertAction(title: "two", style: .Default) { (alert: UIAlertAction!) -> Void in
              NSLog("You pressed button two")
          } // 3
      
          alert.addAction(firstAction) // 4
          alert.addAction(secondAction) // 5
          presentViewController(alert, animated: true, completion:nil) // 6
      
      

      【讨论】:

        【解决方案5】:

        以下是在 iPhone 上显示警报消息的几种方式

        请查看this link for more samples and screenshots

        (包含源代码的 XCode 项目)

        • 简单的操作表
        • 确定/取消操作表
        • 简单提醒

        // 使用确定和取消按钮打开警报

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
                message:@"My message" delegate:self cancelButtonTitle:@"Cancel"
                otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];
        

        【讨论】:

          【解决方案6】:

          您可以使用调试输出(有时您无法使用 NSLog,因为这些错误仅在应用程序在设备上启动时出现,而不是从 Xcode 中出现):

          #define MY_ALERT(str) [[[UIAlertView alloc] initWithTitle:@"System Alert" message:str delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]
          

          然后,在你的代码中你可以这样做,例如:

          MY_ALERT(NSStringFromCGRect(someView.frame))
          

          【讨论】:

            【解决方案7】:
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Hello world" message:@"This is an alert view" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
            

            通过这种方式,我们创建了一个 UIAlertView 类的对象并设置标题“Hello world”和消息“这是一个警报视图”和标题按钮还可以。 详细解答请访问this blog

            【讨论】:

            • 请添加显示警报的方式(否则您的回答无用),我会给您+1 :)
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多