2015-10-02 62 views
2

我試圖用Swift和Parse創建一個應用程序。我使用的Xcode 7和斯威夫特2 我想顯示提示信息時,用戶的登錄失敗,這裏是我的功能:其視圖不在窗口層次結構中的UIAlertController

func logInViewController(logInController: PFLogInViewController, didFailToLogInWithError error: NSError?){ 
    let alertLoginFailed = UIAlertController(title: "Login Failed", message: "Your username or password is invalid!", preferredStyle: UIAlertControllerStyle.Alert) 
    alertLoginFailed.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
    self.presentViewController(alertLoginFailed, animated: true, completion: nil) 
    print("Login failed.........!") 
} 

但在模擬器中運行時,我得到這個錯誤:

2015-10-02 11:32:39.988 RedString[2089:886501] Warning: Attempt to present <UIAlertController: 0x7a934400> on <MyProject.ViewController: 0x7b985150> whose view is not in the window hierarchy! 
Login failed.........! 

我已經使用了它,但我沒有找到明確的解決方案。

這裏是我的全班同學:

import UIKit 
import Parse 
import ParseUI 
class ViewController: UIViewController, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func viewDidAppear(animated: Bool) { 
     self.setupLoginView() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func logInViewController(logInController: PFLogInViewController, didLogInUser user: PFUser){ 
     self.dismissViewControllerAnimated(true, completion: nil) 
    } 

    func logInViewController(logInController: PFLogInViewController, didFailToLogInWithError error: NSError?){ 
     let alertLoginFailed = UIAlertController(title: "Login Failed", message: "Your username or password is invalid!", preferredStyle: UIAlertControllerStyle.Alert) 
     alertLoginFailed.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
     self.presentViewController(alertLoginFailed, animated: true, completion: nil) 
     print("Login failed.........!") 
    } 

    func signUpViewController(signUpController: PFSignUpViewController, didSignUpUser user: PFUser){ 
     self.dismissViewControllerAnimated(true, completion: nil) 
    } 

    func signUpViewController(signUpController: PFSignUpViewController, didFailToSignUpWithError error: NSError?){ 
     print("Sign up failed.........!") 
    } 

    func setupLoginView(){ 
     if(PFUser.currentUser() == nil){ 
      let loginViewController = PFLogInViewController() 
      loginViewController.delegate = self 
      let signUpViewController = PFSignUpViewController() 
      signUpViewController.delegate = self 
      loginViewController.logInView!.logo = UIImageView(image: UIImage(named:"logo.png")) 

      loginViewController.signUpController = signUpViewController 
      self.presentViewController(loginViewController, animated: true, completion: nil) 
     }else{ 
      print("login as: " + PFUser.currentUser()!.username!) 
      //prepare new view here 
     } 

    } 
} 
+0

請參閱此問題http://stackoverflow.com/questions/11862883/whose-view-is-not-in-the-window-hierarchy –

+0

難道是因爲您的viewController不在屏幕上時,loginViewController委託didFailToLogInWithError被調用? – Azzaknight

+0

嘗試使用logInContorller.presentViewController而不是self.presentViewController – Azzaknight

回答

0

你得叫presentViewController:animated:completion登錄視圖控制器上。

下面是一個簡化版本的類的向你展示我的意思:

class ViewController: UIViewController { 
    weak var loginViewController: UIViewController? 

    func setupLoginViewController() { 
     let loginVC = PFLogInViewController() 
     // setup loginVC 
     loginViewController = loginVC // store the reference 
    } 

    func loginDidFail() { 
     let alertVC = UIAlertController(...) 
     // setup alertVC 
     loginViewController?.presentViewController(...) // present the alert from the login view controller 
    } 
} 
+0

非常感謝。 –

+0

沒問題!請將其中一個答案標記爲「已接受」,以便將此問題標記爲已回答。 – joern

0

func logInViewController(logInController: PFLogInViewController, didFailToLogInWithError error: NSError?){ 
     let alertLoginFailed = UIAlertController(title: "Login Failed", message: "Your username or password is invalid!", preferredStyle: UIAlertControllerStyle.Alert) 
     alertLoginFailed.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
     self.presentViewController(alertLoginFailed, animated: true, completion: nil) 
     print("Login failed.........!") 
    } 

被調用,您的視圖控制器是不是在屏幕上。因此,您既可以

  • 駁回logInViewController,則顯示警報,並通過 setUpLogInView的處理程序。

  • 或在上面的功能,嘗試logInController.presentViewController 代替self.presentViewController

+0

@Azzaknigt:非常感謝,它的工作。 –

相關問題