iOS8 UIAlertController
参考:http://www.cocoachina.com/ios/20141126/10320.html
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
/**
* 创建一个UIAlertController对象
* title:标题
* message :提示内容
* 提示类型,和 UIActionSheet 合二为一了
*/
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您尚未输入手机号码" preferredStyle:UIAlertControllerStyleAlert];
/**
* 给UIAlertController对象添加textField控件
*/
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//给提示对象添加第一个textField,并在这里设置
textField.placeholder = @"请输入手机号";
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, textField.frame.size.height)];
leftView.backgroundColor = [UIColor orangeColor];
textField.leftView = leftView;
textField.leftViewMode = UITextFieldViewModeAlways;
//textField.layer.cornerRadius = 5;
//textField.clipsToBounds = YES;
}];
//给提示对象添加第二个textField,并在这里设置
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"请输入交易密码";
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, textField.frame.size.height)];
leftView.backgroundColor = [UIColor orangeColor];
textField.leftView = leftView;
textField.leftViewMode = UITextFieldViewModeAlways;
}];
//添加第一个事件
UIAlertAction *acOk = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"确定");
//UIAlertActionStyleDestructive:破坏性的
//拿到添加的textField
UITextField *textField = alert.textFields.firstObject;
UITextField *lastTextField = alert.textFields.lastObject;
NSLog(@"第一个textfield的文字:%@,第二个textfield的文字:%@",textField.text,lastTextField.text);
}];
//添加第二个事件
UIAlertAction *acCancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"取消");
}];
[alert addAction:acOk];
[alert addAction:acCancle];
[self presentViewController:alert animated:YES completion:nil];
}