UIAlertController的使用(ios9.0后代替UIAlertView与UIActionSheet)

这两天在帮人写demo的时候,使用UIAlertView总是提示警告,看来UIAlertController不用是不行了,所以把使用的方法整理出来大家一起学习。

这篇文章,是看过飞飞大神写的:UIAlertController的一些简单实用方法 之后做了一些补充和整理。



UIAlertController是用来代替之前我们使用的UIAlertView和UIActionSheet,这次的改进总体来讲,感觉思路更清晰简洁了,使用起来也是颇为顺手,下面不多说老样子上代码:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self alertViewcontrol];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)alertViewcontrol

{
    
    //UIAlertControllerStyleAlert
    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"司小文的博客" message:@"http://blog.csdn.net/siwen1990" preferredStyle:UIAlertControllerStyleAlert];
    
    //UIAlertControllerStyleActionSheet
//    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"司小文的博客" message:@"http://blog.csdn.net/siwen1990" preferredStyle:UIAlertControllerStyleActionSheet];

    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //普通按钮
        NSLog(@"我是普通按钮");
    }];
    
    UIAlertAction *aaaAction = [UIAlertAction actionWithTitle:@"aaa" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        //红色按键
        NSLog(@"我是红色按键");
    }];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        //取消按钮
        NSLog(@"我是取消按钮");
    }];
    
    
    //如果是UIAlertControllerStyleActionSheet 不能使用添加输入框的方法
    [alertControl addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        //添加输入框(已经自动add,不需要手动)
        
        textField.text = @"可以在这里写textfield的一些属性";
        
        //监听
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(listeningTextField:) name:UITextFieldTextDidChangeNotification object:textField];
        
    }];
    
    //添加按钮(按钮的排列与添加顺序一样,唯独取消按钮会一直在最下面)
    [alertControl addAction:okAction];//ok
    [alertControl addAction:aaaAction];//aaa
    [alertControl addAction:cancelAction];//cancel
    
    //显示警报框
    [self presentViewController:alertControl animated:YES completion:nil];
    
}

//监听弹框上的输入内容的变化
-(void)listeningTextField:(NSNotification *)notionfication
{
    UITextField *thisTextField = (UITextField*)notionfication.object;
    NSLog(@"%@",thisTextField.text);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


UIAlertController中的显示方式有两种只是在创建时不同,其他的方法基本都是一样的

UIAlertControllerStyleAlert:(有输入框)



UIAlertControllerStyleActionSheet:(无输入框)



UIAlertController可以添加的按钮总共有三类

UIAlertActionStyleDefault 普通

UIAlertActionStyleDestructive 红色

UIAlertActionStyleCancel 取消


主要记得,在改变UIAlertControllerStyleActionSheet的时候一定不要加上输入框,不然会崩溃呦~

感谢观看,学以致用更感谢!


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值