iOS警告与提示对话框

Welcome in my book!

  • 这里首先使用UIAlertController创建一个提示对话框,按照工厂方法即可快速创建,参数UIAlertControllerStyle只有一种样式:UIAlertControllerStyleAlert。
  • 填写完提示的标题和内容后,就可以使用UIAlertAction创建一个具体的按钮行为了。参数UIAlertActionStyle有三种样式, UIAlertActionStyleDefault(普通)、UIAlertActionStyleCancel(取消)、UIAlertActionStyleDestructive(警告(破坏性的))。
    默认状态是正常蓝色字体,取消状态时字体加粗,而警告状态字体则会变为红色。当只添加一个行为对象时,行为对象会显示在UIAlertController对象的下面,添加2个时,并排显示,添加3个及以上者,按列显示。
  • 你可以很方便的在任意一个事件响应函数中,添加以下代码,并在块语句中添加当用户选择相应的选项时执行的语句。
- (IBAction)BtnClick:(UIButton *)myAlertBtn {

    //使用UIAlertController创建一个提示对话框,只有标题和信息
    //UIAlertControllerStyle只有一种样式:UIAlertControllerStyleAlert

    //使用UIAlertAcion创建具体的行为,同时添加三个则按列显示
    //UIAlertActionStyle有三种样式,普通、取消、警告(破坏性的)
    //UIAlertActionStyleDefault、UIAlertActionStyleCancel(字体加粗显示)、UIAlertActionStyleDestructive(字体红色显示)

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"messagebox" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    }];
    UIAlertAction *action2= [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }];
    UIAlertAction *action3= [UIAlertAction actionWithTitle:@"警告" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
    }];

    [alert addAction:action1];
    [alert addAction:action2];
//    [alert addAction:action3];
    [self presentViewController:alert animated:YES completion:^{
    }];

}

你可能感兴趣的:(iOS警告与提示对话框)