程序员iOS技术点

iOS开发之UIAlertController

2016-03-02  本文已影响261人  李小南

如果这篇文章帮助到了您,希望您能点击一下喜欢或者评论,你们的支持是我前进的强大动力.谢谢!

从ios8之后,系统的弹框 UIAlertView 与 UIActionSheet 两个并在一了起, 使用了一个新的控制器叫 UIAlertController

下面我就来介绍以下UIAlertController的简单使用

/*
参数:
Title:显示的标题
message:标题底部显示的描述信息
preferredStyle:弹框的样式
    样式分为两种:UIAlertControllerStyleActionSheet
              : UIAlertControllerStyleAlert
*/
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确定要退出嘛?" message:@“显示的信息" preferredStyle:UIAlertControllerStyleActionSheet]; 
Snip20160302_9.png
/*
参数:
actionWithTitle:按钮要显示的文字
style:按钮要显示的样式
      样式分为三种:
       UIAlertActionStyleDefault:默认样式,默认按钮颜色为蓝色 
       UIAlertActionStyleCancel:设置按钮为取消.点击取消时,会动退出弹框. 
                                注意:取消样式只能设置一个,如果有多个取消样式,则会发生错误.
       UIAlertActionStyleDestructive:危险操作按钮,按钮颜色显示为红色 
       handler:点击按钮时调用Block内部代码.
*/
                   
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 
        NSLog(@"点击了取消"); 
}];

UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { 
        NSLog(@"点击了确定"); 
        [self.navigationController popViewControllerAnimated:YES];
}];
//把创建的UIAlertAction添加到控制器当中:
[alertController addAction:action]; 
[alertController addAction:action1];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { 
     textField.placeholder = @“文本框点位文字"; 
 }];
- 运行效果图:

     ![6080D576-7892-4C75-831F-0088AF2734B1.png](https://img.haomeiwen.com/i1629728/fc905c0e2ff0dcd9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- 通过控制器的textFields属性获取添加的文本框.注意textFields它是一个数组.
UITextField *textF =  alertController.textFields.lastObject;
[self presentViewController:alertController animated:YES completion:nil]; 
上一篇 下一篇

猜你喜欢

热点阅读