UIAlertController实现如图效果
我设置了一个按钮,在按钮的点击事件里写的以下代码:
- (void)action:(UIButton *)btn{
//创建提示框
UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"这里" message:@"成功" preferredStyle:UIAlertControllerStyleAlert];
//创建提示框取消按钮
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"点击的是取消");
}];
//创建提示框OK按钮
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
//把按钮加在提示框上
[ac addAction:cancel];
[ac addAction:ok];
//弹出提示框
[self presentViewController:ac animated:YES completion:^{
}];
}
提示框上也可以加入UITextField,效果如图:
代码如下:
- (void)action:(UIButton *)btn{
//创建提示框
UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"这里" message:@"成功" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"点击的是取消");
}];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//在提示框上创建UITextField
UITextField *username = ac.textFields[0];
UITextField *password = ac.textFields[1];
NSLog(@"username = %@", username);
NSLog(@"password = %@", password);
}];
//将提示框按钮添加到提示框上
[ac addAction:cancel];
[ac addAction:ok];
//在UITextField上添加暗字
[ac addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"用户名";
}];
[ac addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"密码";
}];
//弹出提示框
[self presentViewController:ac animated:YES completion:^{
}];
}