ios - 推新的 View  Controller (以编程方式)不起作用?为什么?-LMLPHP这里我在一个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,动画:true)

在它工作正常。但是为什么推不起作用?

最佳答案

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

如果将self.navigationController?嵌入到UIViewController中,则LoginViewControllerUINavigationController的可选属性,则可以使用UINavigationController方法pushViewController,并且此方法从ViewUSRController推送LoginViewController

因此,如果您使用情节提要,那么将UINavigationController嵌入到LoginViewController中非常简单。

脚步 :-

1.在情节提要中选择LoginViewController
2.xCpode顶部面板“编辑器”
3.选择“嵌入”-> UINavigationController

看起来像这样

ios - 推新的 View  Controller (以编程方式)不起作用?为什么?-LMLPHP

关于ios - 推新的 View Controller (以编程方式)不起作用?为什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43485878/

10-11 19:47