swift - Display an UIAlertView when app launches -
i'm trying add uialertview or controller when app first loads. currently, have code in viewdidload
method.
let welcomealert = uialertcontroller(title: "hola", message: "this test.", preferredstyle: uialertcontrollerstyle.alert) welcomealert.addaction(uialertaction(title: "ok.", style: uialertactionstyle.default, handler: nil)) self.presentviewcontroller(welcomealert, animated: true, completion: nil)
why won't alert view display? used same code inside of ibaction
button, , works expected.
you should displaying alert in viewdidappear:
function. present subview or view controller, parent view controller has in view hierarchy.
the
viewdidappear
function "notifies view controller view added view hierarchy."
so, code this:
class myviewcontroller: uiviewcontroller { override func viewdidappear(animated: bool){ super.viewdidappear(animated) let welcomealert = uialertcontroller(title: "hola", message: "this test.", preferredstyle: uialertcontrollerstyle.alert) welcomealert.addaction(uialertaction(title: "ok.", style: uialertactionstyle.default, handler: nil)) self.presentviewcontroller(welcomealert, animated: true, completion: nil) } }
Comments
Post a Comment