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];
}];
效果: