Note_UIAlertController

UIAlertController作为iOS8新特性,按键的触发方法采用block回调方式实现,一改UIActionSheet和UIAlertView中的delegate模式;

ActionSheet

UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

UIAlertAction * aAction = [UIAlertAction actionWithTitle:@"Default" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

NSLog(@"Default");

}];

UIAlertAction * bAction = [UIAlertAction actionWithTitle:@"Destructive" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

NSLog(@"Destructive");

}];

[alertVC addAction:cancelAction];

[alertVC addAction:aAction];

[alertVC addAction:bAction];

[self presentViewController:alertVC animated:YES completion:nil];

在preferredStyle为ActionSheet模式下,只有添加了UIAlertActionStyleCancel形式的UIAlertAction后,点击背景遮罩才会同时触发该方法并收起弹出窗;

(该特性其实与UIActionSheet是一致的,当cancelButtonTitle设为nil时,点击背景遮罩是无效的)

UIAlertController对象添加UIAlertAction的顺序(Cancle除外)与UIActionSheetDelegate方法中的clickedButtonIndex一致;

Alert

UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

UIAlertAction * aAction = [UIAlertAction actionWithTitle:@"Default" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

NSLog(@"Default");

}];

UIAlertAction * bAction = [UIAlertAction actionWithTitle:@"Destructive" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

NSLog(@"Destructive");

}];

UIAlertAction * cAction = [UIAlertAction actionWithTitle:@"打印文本内容" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

NSLog(@"%@", [alertVC.textFields firstObject].text);

}];

[alertVC addAction:cancelAction];

[alertVC addAction:aAction];

[alertVC addAction:bAction];

[alertVC addAction:cAction];

[alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

textField.delegate = self;

textField.placeholder = @"请输入帐号";

}];

[self presentViewController:alertVC animated:YES completion:nil];

仅在preferredStyle为Alert模式下,可以在弹出框内添加TextField,我们可以为该输入框添加delegate,并通过UIAlertController的属性textFields数组获取某个textField。

tips:通过 

alertVC.view.tintColor = [UIColor orangeColor];

可修改按键文字颜色(对title、message及Destructive按键无效)

Demo:nutletor/Note_UIAlertController · GitHub

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转载 http://www.cocoachina.com/ios/20141126/10320.html如果侵权,...
    小刘_假装是个程序员阅读 662评论 0 0
  • 自从iOS9出来之后,需要使用UIAlertController来弹出弹框,不在提倡使用UIAlertView了,...
    南京杨小兵阅读 574评论 1 0
  • 当升级Xcode到7.2版本后突然发现之前一直写的UIAlertView和UIActionSheet官方不推...
    王少峰阅读 2,770评论 1 2
  • 转自星夜暮晨的博客 对话框样式 1`创建Alert控制器 Objective-C版本: UIAlertContro...
    哇次哟累阅读 1,293评论 0 0
  • 今天我和妈妈一起去乘地铁,由于天气太热,所以我们买了个冰棍就出发了。首先我们先乘地铁二号线到石湖东路站,接着...
    zjdebbie阅读 241评论 0 0