UIAlertController_AlertView

@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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值