objective-c iphone 中的警报视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1747510/
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-09-03 22:30:18  来源:igfitidea点击:

Alert view in iphone

iphoneobjective-ccocoa-touch

提问by shreedevi

I am new to iPhone application development. I want to design an alert view with 2 buttons: OKand Cancel. When the user touches the OKbutton, then I will print a message that says hello. When they touch the Cancelbutton, I will print cancel.

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

Please help; how do I do this?

请帮忙; 我该怎么做呢?

回答by Steve Harrison

To show the alert:

要显示警报:

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];

To respond to whatever button was tapped:

要响应点击的任何按钮:

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

For more information, see the UIAlertView Class Referenceand the UIAlertView Delegate Protocol Reference.

有关更多信息,请参阅UIAlertView 类参考UIAlertView 委托协议参考

回答by krakover

since the chosen answer is deprecated, here is the new solution:

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

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];

As shown in iOs Developer guide.

iOs 开发人员指南中所示。

回答by Niels Castle

Show the alert with the following snippet

使用以下代码段显示警报

UIAlertView *alert = [[UIAlertView alloc]
   initWithTitle:@"Make an informed choice"
   message:nil
   delegate:self
   cancelButtonTitle:@"Cancel"
   otherButtonTitles:@"OK", nil];
[alert show];

The delegate is set to self so when the alert is dismissed our own class will get a call back. The delegate must implement the UIAlertViewDelegate protocol.

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

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

   if (buttonIndex == 1) {
      // Do it!
   } else {
      // Cancel
   }
}

回答by Adrian Pirvulescu

Here are a few ways of showing Alert messages on the iPhone

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

please check this link for more samples and screenshots.

请查看此链接以获取更多示例和屏幕截图

(XCode project with source code included)

(包含源代码的 XCode 项目)

  • Simple Action Sheet
  • OK/Cancel Action Sheet
  • Simple Alert
  • 简单动作表
  • 确定/取消操作表
  • 简单警报

// open a alert with an OK and cancel button

// 打开一个带有 OK 和取消按钮的警报

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

回答by Mr.Javed Multani

enter image description here

在此处输入图片说明

For Objective C:

对于目标 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

For Swift:

对于斯威夫特:

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

回答by Lukas Kalinski

For debug output you could use (sometimes it happens that you can't use NSLog due to bugs that only appear when app is launched on the device and not from Xcode):

对于调试输出,您可以使用(有时由于仅在设备上启动应用程序而不是从 Xcode 启动时才会出现的错误,您无法使用 NSLog):

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

Then, in your code you could do, for example:

然后,在您的代码中,您可以执行以下操作,例如:

MY_ALERT(NSStringFromCGRect(someView.frame))

回答by yasir

UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Hello world" message:@"This is an alert view" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];

In this way we create an object of class UIAlertViewand set the title "Hello world" and the message "This is an alert view" and the title of button as ok. For a detail answer visit this blog

通过这种方式,我们创建了一个类的对象,UIAlertView并将标题“ Hello world”和消息“这是一个警报视图”和按钮的标题设置为ok。有关详细答案,请访问此博客