2017-04-19 50 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

相关问题