iOS UIAlertController控件

ios 9 以后 UIAlertController取代UIAlertView和UIActionSheet

UIAlertControllerStyleAlert和UIAlertControllerStyleActionSheet。

在UIAlertController中添加按钮和关联输入框

UIAlertAction共有三种类型,默认(UIAlertActionStyleDefault)、取消(UIAlertActionStyleCancel)和警告(UIAlertActionStyleDestructive)。

objectivec 复制代码
- (void)addAction:(UIAlertAction *)action;
- (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;

1. UIAlertControllerStyleAlert模式

UIAlertControllerStyleAlert模式会弹出一个对话框视图,点击其他区域不会退出。

objectivec 复制代码
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"这是一个测试"
                                preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    textField.placeholder = @"请输入";
}];
[alert addAction:[UIAlertAction actionWithTitle:@"默认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
    NSLog(@"text = %@", alert.textFields.firstObject.text);
    NSLog(@"默认按钮");
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
    NSLog(@"取消按钮");
}]];
[self presentViewController:alert animated:YES completion:^(){
    NSLog(@"alert completion");
}];

显示如下

2. UIAlertControllerStyleActionSheet模式

UIAlertControllerStyleActionSheet模式会从底部弹出一个视图,点击其他区域也会退出。

objectivec 复制代码
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"这是一个测试"
                                preferredStyle:UIAlertControllerStyleActionSheet];
[alert addAction:[UIAlertAction actionWithTitle:@"选项一" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
    NSLog(@"按钮一");
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"选项二" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
    NSLog(@"按钮二");
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
    NSLog(@"取消按钮");
}]];

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

显示如下

相关推荐
pop_xiaoli8 小时前
【iOS】RunLoop
macos·ios·objective-c·cocoa
区块block11 小时前
iOS 27 重磅开放:第三方 AI 模型自由切换,苹果生态告别封闭
人工智能·ios
人月神话Lee14 小时前
【图像处理】亮度与对比度——图像的线性变换
ios·ai编程·图像识别
bryceZh15 小时前
iOS26适配-UISplitViewController配置分栏和分屏
ios·ui kit
songgeb15 小时前
NumberFormatter 货币格式化属性详解
ios·swift
for_ever_love__19 小时前
UI学习:数据驱动ce l l
学习·ui·ios·objective-c
KillerNoBlood19 小时前
2026移动端跨平台开发面经总结
android·算法·flutter·ios·移动开发·鸿蒙·kmp
人月神话-Lee20 小时前
【图像处理】颜色科学与灰度化——人眼看到的和数字记录的不一样
图像处理·人工智能·计算机视觉·ios·swift
号码认证服务21 小时前
给用户打电话,怎么在对方手机显示为“XX证券”?号码认证办理步骤
android·运维·服务器·ios·智能手机·iphone·webview
MonkeyKing21 小时前
iOS 启动优化实战:pre-main耗时、二进制重排与动态库裁剪全解析
ios