2015-04-16 13 views
2

我正在研究一个原先为iOS 6编写的旧iOS应用程序,它有一些需要更改的UIActionSheets,所以我一直在努力将它们移动到UIAlertControllers ,使用UIAlertActions。这在iPad2模拟器上工作得非常好,但是当在iPad Air(我有权访问的唯一iPad)上测试时,我的UIAlertAction和UIAlertController在创建后直接变为零(在调试器中查看时,它接收创建指针,但是一旦它执行下一行,它将变为空)。这里是一个代码示例:为什么我的UIAlertActions在iPad上无效空气

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

任何想法或帮助将大大appreaciated!

+0

我有一个类似的问题,并通过将对象设置为@属性(强,非原子)而不是局部变量来解决它。 – rob180

+0

您的iPad空中运行什么操作系统? –

+0

@ rob180给了它一个镜头,但我仍然有同样的问题,出于某种原因,这些UI对象的指针在创建后直接变为零。 – Jet

回答

2

UIAlertController适用于iOS8以上版本。使用UIAlertView中和UIActionSheet的前iOS8上

您最好检查一下阉装置回应类,

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 

} 

这不是明智的做法是检查操作系统的部署数量,就像如果8.0等检查如果它对班级的回应是正确的做法。

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

+0

处理,真棒,完美的作品!非常感谢! – Jet

+0

不用麻烦。感谢您接受答案 –