Reputation: 13
I have written code to display a simple alert popup when a button is clicked. When trying the app in simulator iPhone 4s (8.1), it's working as expected, but when trying in simulator iPhone 4s (7.1), the app keeps crashing.
Here the code:
@IBAction func buttonPressed(sender: UIButton) {
let controller = UIAlertController(title: "This is a title", message: "Hello, my friend", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "Phew!", style: .Cancel, handler: nil)
controller.addAction(cancelAction)
self.presentViewController(controller, animated: true, completion: nil)
}
Here's the error message:
The first line code (the one creating the "controller" constant) is highlighted in green with the message "Thread 1: EXC_BAD_ACCESS(Code=1,address=0x10)"
I would appreciate any help
Upvotes: 0
Views: 379
Reputation: 13
Thanks to recommended links from Dom Bryan, I managed the following solution:
@IBAction func buttonPressed(sender: UIButton) {
if NSClassFromString("UIAlertController") == nil{
let alert = UIAlertView(title: "This is a title", message: "I am an iOS7 alert", delegate: self, cancelButtonTitle: "Phew!")
alert.show()
}else{
let controller = UIAlertController(title: "This is a title", message: "Hello, my friend", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Phew!", style: .Cancel, handler: nil)
controller.addAction(cancelAction)
self.presentViewController(controller, animated: true, completion: nil)
}
}
And it's working fine on iOS7 and iOS8 devices
Upvotes: 0
Reputation: 1268
UIAlertController is only available in iOS 8.0 and on wards unfortunately, here is documentation and it states this on the right: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/#//apple_ref/doc/uid/TP40014538-CH1-SW2
I believe this replaced the now deprecated UIAlertView which can be found here in Apple Documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html#//apple_ref/occ/cl/UIAlertView
Swift doesn't like the old but consider and "if" clause and create both a UIAlertView and UIAlertController for the respective iOS system using the app.
Upvotes: 0
Reputation: 15784
UIAlertController is available for iOS >= 8.0
You have to use UIAlertView for iOS < 8.0
Upvotes: 1