我正在开发一个最初为 iOS 6 编写的旧 iOS 应用程序,它有一些需要更改的 UIActionSheets,因此我一直在努力使用 UIAlertActions 将它们移至 UIAlertControllers。这在 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.
任何想法或帮助将不胜感激!
最佳答案
UIAlertController 适用于 iOS8 及以上版本。 iOS8之前使用UIAlertView和UIActionSheet
您最好检查您的设备是否响应该类,
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 等)并不明智,检查它是否响应类才是正确的方法。
它可以防止崩溃意味着您不依赖于不能保证可靠的 float ,就好像它们改变了操作系统将来的命名方式一样,您的代码将会崩溃。
关于ios - 为什么我的 UIAlertActions 在 iPad Air 上变为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29684018/