UIAlertController
代替UIAlertView
和UIActionSheet
的使用
效果图
使用注意:
UIAlertControllerStyleAlert
实现UIAlertView
功能UIAlertControllerStyleActionSheet
实现UIActionSheet
功能
实例化
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"内容" preferredStyle:UIAlertControllerStyleAlert];
添加文本框
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"网络名称";
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
}];
添加按钮
取消按钮
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:cancelAction];
确定按钮
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *textfield = alertController.textFields.firstObject;
}];
[alertController addAction:confirmAction];
显示
[self presentViewController:alertController animated:YES completion:NULL];