2017-04-19 52 views
0

enter image description here在這裏,我在一個viewController,我想去另一個ID爲ViewUSRControllerID的viewController這裏是我的代碼。目標移動我的用戶登錄後:推動新的視圖控制器(編程)不工作?爲什麼?

@IBAction func login_btn(_ sender: UIButton) { 
    if username_txt.text == "" || password_txt.text == "" { 

     //print("empty") 

     let alert = UIAlertController(title: "Error", message: "you must fill up both username and password", preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)) 
     self.present(alert, animated: true, completion: nil) 

    }else if (username_txt.text! == "admin" && password_txt.text! == "Passw0rd"){ 

      print("login successful") 

     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let cr = storyboard.instantiateViewController(withIdentifier: "ViewUSRControllerID") 
     self.navigationController?.pushViewController(cr, animated: true) 


     } 
    } 

輸出是我可以打印login successful,但正在取得任何推動。爲了測試的緣故,我代替:

self.navigationController?.pushViewController(cr, animated: true) 

有: self.present(CR,動畫:真)

在它工作正常。但爲什麼推動不起作用?

+4

是你的控制器內嵌有導航控制器? –

+0

我認爲這是,請再次參考問題。我已經更新了它。 –

+0

我的不好,我已經得到了;) –

回答

1

在你的代碼 self.navigationController?.pushViewController(cr, animated: true)

self.navigationController?UIViewController可選屬性,如果你的LoginViewController嵌有UINavigationController比你可以使用UINavigationController方法pushViewControllerLoginViewController這種方法推ViewUSRController

所以,如果你正在使用故事板比它很簡單,嵌入UINavigationControllerLoginViewController

步驟: -

1.Select在故事板LoginViewController。 2.xCpode頂部面板上的 「編輯」 3.Select 「中嵌入」 - >UINavigationController

它看起來像這樣

enter image description here

相關問題