推送新的视图控制器(以编程方式)不起作用?为什么?
pushing the new view controller (programatically) doesnt work ? why?
我在 viewController 中,我想去另一个 ID 为 ViewUSRControllerID
的 viewController 这是我的代码。 objective 用户登录后移动:
@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,动画:真)
它工作正常。但为什么推动不起作用?
在你的代码中
self.navigationController?.pushViewController(cr, animated: true)
self.navigationController?
是 UIViewController
的可选 属性 如果你的 LoginViewController
嵌入了 UINavigationController
而你可以使用 UINavigationController
方法 pushViewController
并且此方法从 LoginViewController
.
推送 ViewUSRController
因此,如果您使用故事板,那么将 UINavigationController
嵌入到 LoginViewController
中非常简单。
步骤:-
1.Select LoginViewController
在情节提要中。
2.xCpode 顶部面板“编辑器”
3.Select“嵌入”-> UINavigationController
看起来像这样
ViewUSRControllerID
的 viewController 这是我的代码。 objective 用户登录后移动:
@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,动画:真)
它工作正常。但为什么推动不起作用?
在你的代码中
self.navigationController?.pushViewController(cr, animated: true)
self.navigationController?
是 UIViewController
的可选 属性 如果你的 LoginViewController
嵌入了 UINavigationController
而你可以使用 UINavigationController
方法 pushViewController
并且此方法从 LoginViewController
.
ViewUSRController
因此,如果您使用故事板,那么将 UINavigationController
嵌入到 LoginViewController
中非常简单。
步骤:-
1.Select LoginViewController
在情节提要中。
2.xCpode 顶部面板“编辑器”
3.Select“嵌入”-> UINavigationController
看起来像这样