Im trying to show a UIAlertView in my swift App
alert = UIAlertView(title: "",
message: "bla",
delegate: self,
cancelButtonTitle: "OK")
alert!.show()
=> BAD_ACESS error in: -[_UIAlertViewAlertControllerShim initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:] ()
so I suspected self. I set it to nil
alert = UIAlertView(title: "",
message: "bla",
delegate: nil,
cancelButtonTitle: "OK")
alert!.show()
=> ARM_DA_ALIGN error in: -[_UIAlertViewAlertControllerShim initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:] ()
the full code
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UIAlertViewDelegate {
@lazy var window = UIWindow(frame: UIScreen.mainScreen().bounds)
@lazy var locationManager = CLLocationManager()
var alert: UIAlertView? = nil
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
//setup dummy window
self.window.backgroundColor = UIColor.whiteColor()
self.window.rootViewController = UIViewController()
self.window.makeKeyAndVisible()
alert = UIAlertView(title: "",
message: "bla",
delegate: nil,
cancelButtonTitle: "OK")
alert!.show()
return true
}
}
How to do it right? :)
Swift 5
You should do it this way:
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Button", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)