【问题标题】:pushing the new view controller (programatically) doesnt work ? why?推送新的视图控制器(以编程方式)不起作用?为什么?
【发布时间】:2017-04-19 03:35:01
【问题描述】:

这里我在一个 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)

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

【问题讨论】:

  • 您的控制器是否嵌入了导航控制器?
  • 我认为是,请再次参考问题。我已经更新了。
  • 我的错,我明白了;)
  • 您要从哪个控制器推送?

标签: ios swift3 uinavigationcontroller pushviewcontroller


【解决方案1】:

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

self.navigationController?UIViewController 的可选属性,如果您的LoginViewController 嵌入了UINavigationController,那么您可以使用UINavigationController 方法pushViewController,并且此方法从LoginViewController 推送ViewUSRController

因此,如果您使用的是故事板,那么在 LoginViewController 中嵌入 UINavigationController 非常简单。

步骤:-

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

看起来像这样

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 2011-10-05
    • 2020-05-25
    相关资源
    最近更新 更多