OC中,苹果原生底部选择弹窗如何实现,请展示具体代码
时间: 2024-03-17 22:39:39 浏览: 15
苹果原生底部选择弹窗通常使用UIActionSheet或UIAlertController来实现。UIAlertController是iOS8之后引入的新特性,相比于UIActionSheet更加灵活,可以支持多种弹窗样式。
下面是UIAlertController实现底部选择弹窗的代码示例:
```objective-c
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"选项1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 点击选项1后的操作
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"选项2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 点击选项2后的操作
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:action1];
[alertController addAction:action2];
[alertController addAction:cancelAction];
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootViewController presentViewController:alertController animated:YES completion:nil];
```
以上代码中,我们创建了一个UIAlertController对象,并添加了两个选项和一个取消操作。最后,我们将UIAlertController对象展示在当前页面中。