UI第十四节——UIAlertController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *alertBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    alertBtn.frame = CGRectMake(40, 100, 295, 30);
    [alertBtn setTitle:@"Show AlertController" forState:UIControlStateNormal];
    [alertBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:alertBtn];
}

- (void)btnClicked:(UIButton *)btn
{
    // UIAlertControllerStyleActionSheet ActionSheet的样式
    // UIAlertControllerStyleAlert       AlertView的样式
    
    //  实例化UIAlertController,这个东西是iOS8才有的
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"这是一个AlertController" message:@"这里是提示的内容" preferredStyle:UIAlertControllerStyleActionSheet];
    
    // 创建3个按钮
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"按钮1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        NSLog(@"点击了按钮1");
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消按钮" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
        NSLog(@"点击了取消按钮");
    }];
    
    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除按钮" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        
        NSLog(@"点击了删除按钮");
    }];
    
    // 把按钮添加到AlertController里面
    [alertController addAction:action1];
    [alertController addAction:cancelAction];
    [alertController addAction:deleteAction];
    
    // 显示AlertController,用模式跳转的方式让其显示
    [self presentViewController:alertController animated:YES completion:^{
        
    }];
}

如果对你有帮助,请关注我哦!

原文地址:https://www.cnblogs.com/laolitou-ping/p/6244186.html