iOS第九层:UIAlertController的基本使用

Objective_C:

效果1

iOS第九层:UIAlertController的基本使用_第1张图片

OC

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"内容" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self cancel];
    }];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self ok];
    }];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
// 点击「好的」或者「取消」按钮事件
- (void)cancel {
    NSLog(@"取消");
}
- (void)ok {
    NSLog(@"好的");
}

SWIFT

let alertController = UIAlertController(title: "标题", message: "内容", preferredStyle: UIAlertController.Style.alert)
let cancelAction = UIAlertAction(title: "取消", style: UIAlertAction.Style.cancel, handler: {action in self.cancel()})
let okAction = UIAlertAction(title: "好的", style: UIAlertAction.Style.default, handler: {action in self.ok()})
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
// 点击「好的」或者「取消」按钮事件
func ok() {
    print("好的")
}
func cancel() {
    print("取消")
}

效果2(效果2的按钮我没设定事件,设定事件方法参考效果1)

iOS第九层:UIAlertController的基本使用_第2张图片

OC

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"内容" preferredStyle: UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:nil];
UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:deleteAction];
[alertController addAction:archiveAction];
[self presentViewController:alertController animated:YES completion:nil];

SWIFT

let alertController = UIAlertController(title: "标题", message: "内容", preferredStyle: UIAlertController.Style.actionSheet)
let cancelAction = UIAlertAction(title: "取消", style: UIAlertAction.Style.cancel, handler: nil)
let deleteAction = UIAlertAction(title: "删除", style: UIAlertAction.Style.destructive, handler: nil)
let archiveAction = UIAlertAction(title: "保存", style: UIAlertAction.Style.default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(deleteAction)
alertController.addAction(archiveAction)
self.present(alertController, animated: true, completion: nil)

下载Demo地址:Github

【原创:iOS第九层】

你可能感兴趣的:(iOS第九层:UIAlertController的基本使用)