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

时间:2022-09-25 14:43:13

I'm working on an old iOS app originally written for iOS 6, and it had some UIActionSheets that needed to be changed, so I've been working to move them over to UIAlertControllers, using UIAlertActions. This has worked perfectly fine on a iPad2 Simulator, however when testing on an iPad Air (the only iPad I have access to) my UIAlertAction, and UIAlertController become nil directly after being created (looked at in the debugger, it receives a pointer on creation, however as soon as it executes the next line it becomes null). Here's a code sample:

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

//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.

Any thoughts or help would be much appreaciated!

任何想法或帮助都会有很大帮助!

1 个解决方案

#1


2  

UIAlertController is for iOS8 upwards. Use UIAlertView and UIActionSheet for prior to iOS8

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

You are best to check wether your device responds to the 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

}

It's not wise to check the operating system deployment number, like if 8.0 etc, checking the if it responds to the class is the proper way to do it.

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

It prevents a crash means you're not relying on float numbers which are not guaranteed to be reliable, as if they change the way the operating systems is named in future, your code would crash.

它可以防止崩溃意味着您不依赖于不保证可靠的浮点数,就好像它们改变了将来命名操作系统的方式一样,您的代码会崩溃。

#1


2  

UIAlertController is for iOS8 upwards. Use UIAlertView and UIActionSheet for prior to iOS8

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

You are best to check wether your device responds to the 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

}

It's not wise to check the operating system deployment number, like if 8.0 etc, checking the if it responds to the class is the proper way to do it.

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

It prevents a crash means you're not relying on float numbers which are not guaranteed to be reliable, as if they change the way the operating systems is named in future, your code would crash.

它可以防止崩溃意味着您不依赖于不保证可靠的浮点数,就好像它们改变了将来命名操作系统的方式一样,您的代码会崩溃。