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];

显示如下

相关推荐
杂雾无尘29 分钟前
iOS 分享扩展(四):让分享扩展与主应用无缝衔接
ios·swift·apple
烈焰晴天36 分钟前
新发布的一款使用ReactNative新架构加载Svga动画的开源插件[android/ios]
android·react native·ios
前端与小赵38 分钟前
ios如何把H5网页变成主屏幕webapp应用
ios·web app
章鱼paul帝3 小时前
iOS 启动优化之自注册--attribute(section)
ios·性能优化
Larva3 小时前
记录使用 SwiftLint检测代码内的硬编码字符串
ios·swift·代码规范
依旧风轻6 小时前
ChangeNotifierProvider 本质上也是 Widget
flutter·ios·dart·widget·changenotifier·provider·sqi
杂雾无尘20 小时前
iOS 分享扩展(三):轻松定制 iOS 分享界面,提升用户体验
ios·swift·apple
zjam933321 小时前
iOS 26 又有新的属性监听方法啦?
ios
鹤渺1 天前
React Native 搭建iOS与Android开发环境
android·react native·ios
陈大左1 天前
uniapp的更新流程【安卓、IOS、热更新】
android·ios·uni-app