iOS8以后苹果就推荐用UIAlertController来创建提示框了,UIAlertCotroller也是iOS8以后才可以用的
{
// 创建一个alertContoller对象,其中Style参数是传入你要创建的是alertView还是ActionSheet
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"签名" message:@"\n" preferredStyle:UIAlertControllerStyleAlert];
// 取消按钮 UIAlertActionStyleCancel
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"我是取消按钮");
}];
// 其他按钮 UIAlertActionStyleDefault
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"我是确定按钮");
}];
// 描述的红色按钮 UIAlertActionStyleDestructive
UIAlertAction *destructive = [UIAlertAction actionWithTitle:@"描述" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"我是红色按钮");
}];
// 将创建的alertAction添加到alertController中,其中取消按钮一直都是在最底部,其他按钮的顺序按添加顺序
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[alertController addAction:destructive];
// 添加textField输入框,可以在block中修改textField的属性
__weak ViewController *wself = self;
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.backgroundColor = [UIColor redColor];
[textField addTarget:wself action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
textField.placeholder = @"请输入用户名";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.secureTextEntry = YES;
textField.placeholder = @"请输入密码";
}];
// 展示alertController
[self presentViewController:alertController animated:NO completion:^{
}];
}
// 检测textField的文本变化
- (void)textFieldDidChange:(UITextField *)textField
{
NSLog(@"%@", textField.text);
}
其中如果是ActionSheet的话,不能添加textField属性