UIAlertController

本文详细介绍如何使用 iOS 的 UIAlertController 创建各种类型的提示框,包括 alertView 和 ActionSheet,并展示了如何添加不同样式的按钮及文本输入框。

iOS8以后苹果就推荐用UIAlertController来创建提示框了,UIAlertCotroller也是iOS8以后才可以用的

{

    // 创建一个alertContoller对象,其中Style参数是传入你要创建的是alertView还是ActionSheet

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"签名" message:@"\n"         preferredStyle:UIAlertControllerStyleAlert]; 


    // 取消按钮 UIAlertActionStyleCancel

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancelhandler:^(UIAlertAction *action) {

        NSLog(@"我是取消按钮");

    }];

    

    // 其他按钮 UIAlertActionStyleDefault

    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *action) {

        NSLog(@"我是确定按钮");

    }];

    

    // 描述的红色按钮 UIAlertActionStyleDestructive

    UIAlertAction *destructive = [UIAlertAction actionWithTitle:@"描述" style:UIAlertActionStyleDestructivehandler:^(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属性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值