2016-03-14 83 views
1

我想改變視圖控制器,如果登錄成功,但我不確定如何做到這一點。這是我到目前爲止所嘗試過的。提前致謝!登錄後,打開一個新的視圖控制器警報迅速2.0

@IBAction func signinaction(sender: AnyObject) { 
    let user = self.usernamefield.text! 

    ref.authUser(emailfield.text, password: passwordfield.text, withCompletionBlock: { error, authData in 

     if error != nil 
     { 
      let alert = UIAlertController(title: "Error", message: "Enter Email and Password.", preferredStyle: UIAlertControllerStyle.Alert) 
      let action = UIAlertAction(title: "Ok", style: .Default, handler: nil) 
      alert.addAction(action) 
      self.presentViewController(alert, animated: true, completion: nil) 
      print("can not sign in") 
     } 
     else 
     { 

      let storyboard = UIStoryboard(name: "Main", bundle: nil); 
      let viewName:NSString = "NewView" 
      let vc = storyboard.instantiateViewControllerWithIdentifier(viewName as String) as! HomeViewController 
      let uid = authData.uid 
      print("Success with user: \(uid)") 
      let alert = UIAlertController(title: "Success", message: "Welcome \(user)", preferredStyle: UIAlertControllerStyle.Alert) 
      let action = UIAlertAction(title: "Ok", style: .Default, handler: nil) 
      alert.addAction(action) 
      self.navigationController?.pushViewController(vc as HomeViewController, animated: true) 

     } 

    }) 
} 

回答

0

假設視圖控制器所有這些都包含在導航控制器中,你應該正常工作。但請注意,您正在創建一個永不顯示的提醒。我的猜測是,你要顯示的成功警報,然後打開新的視圖控制器,是這樣的:

  let alert = UIAlertController(title: "Success", message: "Welcome \(user)", preferredStyle: UIAlertControllerStyle.Alert) 
      let action = UIAlertAction(title: "Ok", style: .Default) { _ in 
       self.navigationController?.pushViewController(vc, animated: true) 
      } 
      alert.addAction(action) 

      self.presentViewController(alert, animated: true, completion: nil) 

如果仍然不能正常工作,我會確保當前視圖控制器在實際顯示一個導航控制器。

0

看起來您只需要在警報操作中添加導航代碼即可。目前你的處理器參數設置爲nil

let action = UIAlertAction(title: "Ok", style: .Default, handler: nil) 

成爲該

let alertAction = UIAlertAction(title: "Ok", style: .Default) { (action) -> Void in 
     self.navigationController?.pushViewController(vc as HomeViewController, animated: true) 

    } 
相關問題