在iPhone的警报视图

时间:2009-11-17 09:10:25

标签: iphone objective-c cocoa-touch

我是iPhone应用程序开发的新手。我想设计一个包含2个按钮的警报视图:OKCancel。当用户触摸OK按钮时,我会打印一条显示hello的消息。当他们触摸Cancel按钮时,我会打印cancel

请帮忙;我该怎么做?

7 个答案:

答案 0 :(得分:63)

显示提醒:

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

答案 1 :(得分:26)

由于所选答案已弃用,以下是新解决方案:

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所示。

答案 2 :(得分:5)

使用以下代码段显示提醒

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

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

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

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

答案 3 :(得分:2)

以下是一些在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];

答案 4 :(得分:0)

对于您可以使用的调试输出(有时会发生无法使用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))

答案 5 :(得分:0)

Codesandbox

对于目标C

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"My Alert"
            message:@"This is an action sheet." 
            preferredStyle:UIAlertControllerStyleActionSheet]; // 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

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

答案 6 :(得分:-1)

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

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