iOS开发之UIAlertController使用

iOS8之后,Xcode提供的提醒框架启用UIAlertController,用法跟之前差别还有的,见效果图和代码.

demo演示

1.样式一:


   self.alertVc  = [UIAlertController alertControllerWithTitle:@"标题一" message:@"这里是要显示的信息" preferredStyle:UIAlertControllerStyleAlert];
    
    //弹出视图,使用UIViewController的方法
    [self presentViewController:self.alertVc animated:YES completion:^{
        
        //隔一会就消失
        [self dismissViewControllerAnimated:YES completion:^{

        }];
    }];

2.样式二:


 self.alertVc  = [UIAlertController alertControllerWithTitle:@"标题二" message:@"这里是要显示的信息" preferredStyle:UIAlertControllerStyleActionSheet];
    
    //弹出视图,使用UIViewController的方法
    [self presentViewController:self.alertVc animated:YES completion:^{
        
        //隔一会就消失
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }];

3.样式三:



 self.alertVc = [UIAlertController alertControllerWithTitle:@"标题三" message:@"这是标题内容" preferredStyle:UIAlertControllerStyleActionSheet];//UIAlertControllerStyleActionSheet
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];

#warning  修改字体颜色,属于私有Api 慎用
    [cancelAction setValue:[UIColor redColor] forKey:@"_titleTextColor"];[self.alertVc addAction:cancelAction];
    [self.alertVc addAction:sureAction];
    
    //弹出视图,使用UIViewController的方法
    [self presentViewController:self.alertVc animated:YES completion:^{
        
        
    }];

4.样式四:


 self.alertVc = [UIAlertController alertControllerWithTitle:@"标题四" message:@"标题内容" preferredStyle:UIAlertControllerStyleAlert];
    
    [self.alertVc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        textField.placeholder = @"请输入用户名";
    }];
    
    [self.alertVc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        textField.placeholder = @"请输入密码";
        textField.secureTextEntry = YES;
    }];
    
    //对UITextField开始监听
    [self.alertVc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        textField.placeholder = @"输入可以监听到";
        _textField = textField;
        
#warning 监听输入,可以使用通知,可以kvo等,如果是下边的这种方式,容易retain,造成循环引用,所以声明一个全局变量textField,然后在block监听,就不会造成循环引用(capturing 'self'strong in this block is likely to lead to a retain cycle)
//       [textField addTarget:self action:@selector(watchTextFieldMethod:) forControlEvents:UIControlEventEditingChanged];
    }];
    
    
    [_textField addTarget:self action:@selector(watchTextFieldMethod:) forControlEvents:UIControlEventEditingChanged];
    

    
    //添加确定和取消按钮
    UIAlertAction *cacleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];

    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    [self.alertVc addAction:cacleAction];
    [self.alertVc addAction:sureAction];
    
    
    [self presentViewController:self.alertVc animated:YES completion:^{
        
    }];

5.提醒:如果有需要修改字体颜色等,代码有标注,因为涉及到调用私有API,这里就不演示

demo下载:https://github.com/OwenJoe/UIAlertController.git

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

推荐阅读更多精彩内容