@interface ViewController ()<UIAlertViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
/*
//旧的方式来创建
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"我是警告框"
message:@"我是警告狂的内容"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"确定",@"第三个按钮",nil];
//alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alertView show];
*/
//IOS8之后创建方式
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"我是警告框"
message:@"内容"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"第1个按钮"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action){
NSLog(@"我是第1个按钮");
}];
UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"第2个按钮"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action){
NSLog(@"我是第2个按钮");
}];
UIAlertAction *alertAction3= [UIAlertAction actionWithTitle:@"第3个按钮"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * _Nonnull action){
NSLog(@"我是第3个按钮");
}];
UIAlertAction *alertAction4= [UIAlertAction actionWithTitle:@"第4个按钮"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action){
NSLog(@"我是第4个按钮");
}];
//添加按钮
[alertController addAction:alertAction1];
[alertController addAction:alertAction2];
[alertController addAction:alertAction3];
[alertController addAction:alertAction4];
alertController.preferredAction = alertController.actions.lastObject;//首选 高亮
//添加输入框
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField){
textField.placeholder=@"默认文字";
textField.backgroundColor = [UIColor grayColor];
}];
[self presentViewController:alertController animated:YES completion:nil];//把它放在上面是的话添加输入框不起作用的
}
//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
// NSLog(@"%d",buttonIndex);
//}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
UIAlertController_AlertView
最新推荐文章于 2021-06-25 16:46:00 发布