UIAlertController的基本使用

UIActionSheet和UIAlertView都被弃用了, 想要显示弹窗或者actionSheet可以使用UIAlertController,UIAlertController兼具二者功能,并且能做的更多,本文只演示如何显示一个actionSheet。

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确定退出?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
UIAlertAction *aAction1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"点击了取消");
}];

UIAlertAction *aAction2 = [UIAlertAction actionWithTitle:@"确定退出" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"点击了确定");
    [self.navigationController popViewControllerAnimated:YES];
}];

[alertController addAction:aAction1];
[alertController addAction:aAction2];

[self presentViewController:alertController animated:YES completion:nil];

效果图

UIAlertController