OGeek|极客世界-中国程序员成长平台

标题: ios - 为什么我的 UIAlertActions 在 iPad Air 上变为零 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:05
标题: ios - 为什么我的 UIAlertActions 在 iPad Air 上变为零

我正在开发一个最初为 iOS 6 编写的旧 iOS 应用程序,它有一些需要更改的 UIActionSheets,所以我一直在努力将它们移到 UIAlertControllers,使用 UIAlertActions。这在 iPad2 模拟器上运行得非常好,但是在 iPad Air(我可以访问的唯一 iPad)上测试时,我的 UIAlertAction 和 UIAlertController 在创建后直接变为 nil(在调试器中查看,它在创建时收到一个指针,但是一旦它执行下一行,它就会变为空)。这是一个代码示例:

//When hovering over the next line, alert has a pointer
UIAlertController* alert = [UIAlertController 
                           alertControllerWithTitle"info"
                           message"testmessage"
                           preferredStyle:UIAlertControllerStyleActionSheet];
//alert pointer is now nil
//test pointer shows up 
UIAlertAction* test= [UIAlertAction
                        actionWithTitle"I'm a Button"
                        style:UIAlertActionStyleDefault
                        handler:^(UIAlertAction * action){
                          [alert dismissViewControllerAnimated: YES completion:nil];
                        }
                      ];
//test pointer is nil, test2 pointer exists
UIAlertAction* test2 = [UIAlertAction
                        actionWithTitle"I'm a Button"
                        style:UIAlertActionStyleDefault
                        handler:^(UIAlertAction * action){
                          [alert dismissViewControllerAnimated: YES completion:nil];
                        }
                      ];
//test2 pointer is nil
[alert addAction:test];
[self presentViewController:alert animated:YES completion:nil]; //app crashes, because I'm trying to present a nil modal.

任何想法或帮助将不胜感激!



Best Answer-推荐答案


UIAlertController 适用于 iOS8 以上。在 iOS8 之前使用 UIAlertView 和 UIActionSheet

您最好检查您的设备是否响应类(class),

if ([UIAlertController class]) {
   // use UIAlertController for the action sheets as you have already posted

//    For all operating systems (like iOS8 upwards) will land in here. 

} else {
  // use UIActionSheet - like you already used for iOS6

}

检查操作系统部署编号是不明智的,例如 if 8.0 等,检查它是否响应类是正确的方法。

它可以防止崩溃意味着您不依赖 float ,这些 float 不能保证是可靠的,如果它们改变了将来操作系统命名的方式,您的代码就会崩溃。

关于ios - 为什么我的 UIAlertActions 在 iPad Air 上变为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29684018/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4