• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 为什么我的 UIAlertActions 在 iPad Air 上变为零

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:05:08 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在开发一个最初为 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap