UIAlertController

- (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.
}

posted @ 2015-11-04 16:12  古道西风yz  阅读(195)  评论(0)    收藏  举报