UIAlertViewController详解

随着Xcode7的更新,苹果推荐使用UIAlertViewController替代旧的UIAlertView和UIActionSheet。这个新的视图控制器基于UIViewController,通过设置风格展示AlertView或ActionSheet。通过添加AlertAction添加按钮。下面是一个简单的使用示例。

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

Xcode7更新后,苹果不建议使用以前的AlertView和ActionSheet了,而是将二者合一,创建了一个新的视图控制器UIAlertViewController,UIAlertViewController继承自UIViewController,通过改变设置不同的UIAlertControllerStyle来确定显示的是AlertView或者是ActionSheet.通过添加AlertAction来增加按钮,话不多说,贴上自己写的一个小Demo.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self CreateUI];
}

- (void)CreateUI
{
    UIButton * button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button1 setFrame:CGRectMake(100, 100, 100, 50)];
    [button1 setTitle:@"ActionSheet" forState:UIControlStateNormal];
    button1.backgroundColor = [UIColor blackColor];
    [button1 addTarget:self action:@selector(ShowActionSheet) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    UIButton * button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button2 setFrame:CGRectMake(100, 180, 100, 50)];
    [button2 setTitle:@"AlertView" forState:UIControlStateNormal];
    button2.backgroundColor = [UIColor blackColor];
    [button2 addTarget:self action:@selector(ShowAlertView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
}
- (void)prepareAlertCWithStyle:(UIAlertControllerStyle)alertstyle
{
    UIAlertController *_alertC = [UIAlertController alertControllerWithTitle:@"这是AlertController的测试" message:@"iOS8以后用UIAlertController" preferredStyle:alertstyle];
    // 添加取消按钮UIAlertActionStyleCancel
    UIAlertAction *canleaction = [UIAlertAction actionWithTitle:@"取消按钮" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
        NSLog(@"点击了取消按钮");
    }];
    
    [_alertC addAction:canleaction];
    
    // 添加普通按钮UIAlertActionStyleDefault
    UIAlertAction *defaultaction = [UIAlertAction actionWithTitle:@"普通按钮" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        NSLog(@"点击了普通按钮");
    }];
    [_alertC addAction:defaultaction];
    // 添加文本框,只能在alertview模式下添加textfiled
    if (alertstyle == UIAlertControllerStyleAlert) {
        
        
        [_alertC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            // 这里可以对文本框进行一些自定义操作
            textField.placeholder = @"请输入.....";
            
        }];
    }
    // 添加UIAlertActionStyleDestructive样式按钮
    UIAlertAction *replaceaction = [UIAlertAction actionWithTitle:@"销毁按钮" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) {
        if (alertstyle == UIAlertControllerStyleAlert) {
            NSLog(@"%@",_alertC.textFields[0].text);
        }
        NSLog(@"点击了销毁样式按钮");
        
    }];
    
    [_alertC addAction:replaceaction];
    // 显示alertview,UIAlertController是继承与UIViewController的
    [self presentViewController:_alertC animated:YES completion:nil];
}

- (void)ShowActionSheet
{
    // 设置为ActionSheet样式
    [self prepareAlertCWithStyle:UIAlertControllerStyleActionSheet];
}

- (void)ShowAlertView
{
    // 设置为AlertView样式
    [self prepareAlertCWithStyle:UIAlertControllerStyleAlert];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值