objective c - iOS 8 Swift 1.2 and Parse - Attempt to present UIAlertController on ViewController whose view is not in the window hierarchy -
there many questions on stack concerning issue, none of them seem solve issue i'm having.
i using parseui login , signup portion of application. like have happen uialertcontroller
presented when user (for example) not enter in text in username , password fields.
here code masterviewcontroller
:
class masterviewcontroller: uiviewcontroller, pfloginviewcontrollerdelegate, pfsignupviewcontrollerdelegate { override func viewdidload() { super.viewdidload() // additional setup after loading view. } override func viewdidappear(animated: bool) { super.viewdidappear(animated) if (pfuser.currentuser() == nil) { var loginviewcontroller = loginviewcontroller() loginviewcontroller.delegate = self var signupviewcontroller = signupviewcontroller() signupviewcontroller.delegate = self loginviewcontroller.signupcontroller = signupviewcontroller self.presentviewcontroller(loginviewcontroller, animated: true, completion: nil) } } func loginviewcontroller(logincontroller: pfloginviewcontroller, shouldbeginloginwithusername username: string, password: string) -> bool { if (!username.isempty || !password.isempty) { return true } else { let alertcontroller = uialertcontroller(title: "failed login.", message: "login failure. please try again.", preferredstyle: .alert) let defaultaction = uialertaction(title: "ok", style: .default, handler: nil) alertcontroller.addaction(defaultaction) self.presentviewcontroller(alertcontroller, animated: true, completion: nil) return false } } func loginviewcontroller(logincontroller: pfloginviewcontroller, didfailtologinwitherror error: nserror?) { println("failed log in.") } func signupviewcontroller(signupcontroller: pfsignupviewcontroller, shouldbeginsignup info: [nsobject : anyobject]) -> bool { if let password = info["password"] as? string { return count(password.utf16) >= 8 } else { return false } } func signupviewcontroller(signupcontroller: pfsignupviewcontroller, didfailtosignupwitherror error: nserror?) { println("failed sign up.") } func loginviewcontroller(logincontroller: pfloginviewcontroller, didloginuser user: pfuser) { let installation = pfinstallation.currentinstallation() installation["user"] = pfuser.currentuser() installation.saveinbackground() self.dismissviewcontrolleranimated(true, completion: nil) } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func signupviewcontrollerdidcancelsignup(signupcontroller: pfsignupviewcontroller) { println("user dismissed signup.") } }
after reading user's answer seemed answer, added following class workspace:
import foundation class alerthelper: nsobject { func showalert(fromcontroller controller: masterviewcontroller) { var alert = uialertcontroller(title: "abc", message: "def", preferredstyle: .alert) controller.presentviewcontroller(alert, animated: true, completion: nil) } }
and modified method accordingly:
func loginviewcontroller(logincontroller: pfloginviewcontroller, shouldbeginloginwithusername username: string, password: string) -> bool {
if (!username.isempty || !password.isempty) { return true } else { let alertcontroller = uialertcontroller(title: "failed login.", message: "login failure. please try again.", preferredstyle: .alert) let defaultaction = uialertaction(title: "ok", style: .default, handler: nil) alertcontroller.addaction(defaultaction) var alert = alerthelper() alert.showalert(fromcontroller: self) return false }
}
at point i'm not quite sure else do. 1 thought had programmatically add uilabel
loginviewcontroller
, signupviewcontroller
, change content based on errors (or lack thereof) user login , signup, have alerts.
edit: code in loginviewcontroller
. subclassed in order customize appearance. code signupviewcontroller
identical.
import uikit import parse import parseui
class loginviewcontroller: pfloginviewcontroller { override func viewdidload() { super.viewdidload() view.backgroundcolor = ckpurple let label = uilabel() label.textcolor = uicolor.whitecolor() label.text = "welcome." label.sizetofit() loginview?.logo = label // additional setup after loading view. }
the problem present login view controller master view controller, removes master view controller view hierarchy. attempt present alert view controller master view controller, need present view controller in view hierarchy. try presenting alert from login view controller instead.
logincontroller.presentviewcontroller(alertcontroller, animated: true, completion: nil)
Comments
Post a Comment