UIAlertController的使用

本文介绍了iOS 8中新引入的UIAlertController组件,该组件整合了UIAlertView和Action Sheet的功能,并提供了更强大的自定义能力。通过示例代码展示了如何创建带有输入框的警告视图以及如何构建包含不同类型的操作按钮的Action Sheet。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UIAlertController是iOS8出的,他把AlertView和sheet整合到一起了
他的自定义性比alertView要好的多。

不多说直接看代码吧

- (IBAction)AlertNew:(id)sender{
// UIAlertControllerStyleAlert
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"UIAlertController Demo" preferredStyle:UIAlertControllerStyleAlert];

    [alertVC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder =
@"登录";
       
       
//block里可以为控件添加相应的操作
       
//监听
        [[NSNotificationCenter defaultCenter] addObserver:
self selector:@selector(alertTextFieldValueChange:) name:UITextFieldTextDidChangeNotification object:textField];
    }];
   
    [alertVC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder =
@"密码";
        textField.secureTextEntry =
YES;
       
       
    }];
   
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:
@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
       
        [[NSNotificationCenter defaultCenter] removeObserver:
self name:UITextFieldTextDidChangeNotification object:nil];
    }];
    okAction.enabled =
NO;
   
    //添加
    [alertVC addAction:okAction];
   
    //显示,现在是Controller,所以模态让他显示
    [self presentViewController:alertVC animated:YES completion:^{
       
    }];
}

- (IBAction)SheetNew:(id)sender {
   
   
//枚举为 UIAlertControllerStyleActionSheet
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:
@"提示" message:@"信息" preferredStyle:UIAlertControllerStyleActionSheet];
   
   
//如果上拉菜单中有取消(类型)”按钮的话,那么它永远都会出现在菜单的底部,不管添加的次序是如何
   
//Cancel类型
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:
@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
       
    }];
   
//Default类型
    UIAlertAction *save = [UIAlertAction actionWithTitle:
@"选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
       
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
       
//设置代理人,需要签两份协议,一个是UINavigationControllerDelegate, 另一个是UIImagePickerControllerDelegate
        picker.delegate =
self;
       
       
//设置允许进行修改
        picker.allowsEditing =
YES;
       
       
//设置模态跳转
        [picker setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
        [
self presentViewController:picker animated:YES completion:^{
           
        }];
       
       
    }];
   
//Destructive类型,为红色
    UIAlertAction *delete = [UIAlertAction actionWithTitle:
@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
       
    }];
   
//添加
    [alertVC addAction:cancel];
    [alertVC addAction:save];
    [alertVC addAction:delete];
   
   
//显示
    [
self presentViewController:alertVC animated:YES completion:^{
       
    }];
   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值