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

显示如下

相关推荐
貂蝉空大1 小时前
uni-app开发安卓和ios app 真机调试
android·ios·uni-app
胖虎12 小时前
iOS 中的圆角与平滑圆角:从新特性到老项目适配
ios·圆角·平滑圆角·cornercurve
志飞2 小时前
ios UICollectionView使用自定义UICollectionViewCell
ios·collectionview·自定义cell
Neo Evolution8 小时前
Flutter与移动开发的未来:谷歌的技术愿景与实现路径
android·人工智能·学习·ios·前端框架·webview·着色器
没头脑的ht1 天前
ios App的启动过程和启动优化
ios
敲代码的鱼哇1 天前
设备唯一ID获取,支持安卓/iOS/鸿蒙Next(uni-device-id)UTS插件
android·ios·uniapp·harmonyos
江上清风山间明月1 天前
Flutter最简单的路由管理方式Navigator
android·flutter·ios·路由·页面管理·navigator
fangcaojushi1 天前
解决videojs在ios端视频无法播放的问题
ios·音视频
调皮的芋头2 天前
iOS各个证书生成细节
人工智能·ios·app·aigc
coooliang2 天前
【iOS】SwiftUI状态管理
ios·swiftui·swift