- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"警告" message:@"一大波僵尸在靠近"
preferredStyle:UIAlertControllerStyleAlert];
//添加按钮
__weak typeof(alert) weakAlert = alert;
[alert addAction:[UIAlertAction actionWithTitle:@"确定"
style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull
action) {
NSLog(@"点击了确定按钮---%@---%@",[weakAlert.textFields.firstObject text],[weakAlert.textFields.lastObject text]);
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消"
style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull
action) {
NSLog(@"点击了取消按钮");
}]];
//添加文本框
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.text = @"123";
//监听文本框输入的文字内容
[textField addTarget:self action:@selector(usernameChange:) forControlEvents:UIControlEventAllEditingEvents];
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.secureTextEntry = YES;
textField.text = @"456";
//监听文本框输入的内容
[textField addTarget:self action:@selector(passwordChande:) forControlEvents:UIControlEventAllEditingEvents];
}];
[self presentViewController:alert animated:YES completion:nil];
}
-(void)usernameChange:(UITextField *)username
{
NSLog(@"%@",username.text);
}
-(void)passwordChande:(UITextField *)password
{
NSLog(@"%@",password.text);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
原文:http://www.cnblogs.com/yzjxdz/p/4936335.html