iOS -iOS9中提示框(UIAlertController)的常见使用

iOS 8 以前提示框主要使用 UIAlertView和UIActionSheet;iOS 9 将UIAlertView和UIActionSheet合二为一为:UIAlertController 。学习

这下让已经习惯的我一会儿变的不习惯,这样也好,正好再学习一些新的东西;spa

先上一段代码:3d

 

-(void)setupReminder {
        //STEP 1
        NSString *title = @"提示";
        NSString *message = @"请输入用户名和密码";
        NSString *okButtonTitle = @"OK";
    
        //step 2 action
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    
        UIAlertAction *okCtrl = [UIAlertAction actionWithTitle:okButtonTitle
                                                     style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * _Nonnull action){
                                                   }];
        //step 3 action
        [alertController addAction:okCtrl];
        [self presentViewController:alertController animated:YES completion:nil];
    
}

这是最普通的一个alertcontroller,一个ok按钮。code

 

显示效果:orm


若是UIAlertAction *otherAction这种otherAction >2时,它会自动排列成以下
blog



有时须要在alertcontroller中添加一个输入框,例如输入验证码等:
it

 

这时候能够添加以下代码:io

 [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {验证码

        //you can set textfield attribute hereclass

//add backgroundColor

        textField.backgroundColor = [UIColor grayColor];

  }];

效果:

相关文章
相关标签/搜索