【问题标题】:Instantiate view, then navigate away实例化视图,然后导航离开
【发布时间】:2016-01-08 21:54:59
【问题描述】:

我目前正在编写一个应用程序,我们将在用户第一次打开应用程序时启动演练。最后,我们要求用户填写一些详细信息,为此我希望他按一个按钮以重定向到设置页面。

问题是,此页面位于导航控制器的下一层(从登录页面开始)。就目前而言,我可以正确地实例化登录页面,但永远不会重定向到设置页面。

let mainView = self.storyboard?.instantiateViewControllerWithIdentifier("NavCtrl")
self.presentViewController(mainView!, animated: false, completion: nil)
// the above works correctly and sends us to the landing screen 
// (rootView of the navigation controller)

// the following lines never have any effect though
let settingsView = self.storyboard?.instantiateViewControllerWithIdentifier("Settings View")
self.navigationController?.pushViewController(settingsView!, animated: false)

我认为这是因为我试图在故事板有时间启动第一个或第二个视图之前调用 .pushViewController

所以我有几个问题:

  • 有没有办法,确实,实例化一个视图,然后在它被实例化后立即导航到另一个视图? (这是为了保持导航堆栈并保持准确的导航栏行为)
  • 如果没有,是否可以以编程方式填充导航堆栈,以便我只需要实例化设置视图?这是为了让导航栏中的后退按钮仍然可以发送到登陆屏幕?

提前谢谢大家

【问题讨论】:

  • 当您执行self.navigationController?.pushViewController(settingsView!, animated: false) 时,您确定self.navigationController? 不为零吗?主视图控制器未显示在导航堆栈上,而是以模态显示
  • 在您的第一个视图控制器的 viewDidLoad 中,您可以将 self.performsegue... 添加到另一个视图
  • 你执行了两次。 1ce 'presentViewController' 然后是 'self.navigationController?.pushViewController'。正如你所提到的,下面的 2 行永远不会被执行。您应该做的就是在您最初推送到的navigationController 的“viewDidLoad”中设置一个函数,该函数可以实例化到您希望它推送到的另一个VC。要拥有后退按钮,请将引导嵌入到 NavigationController 中。
  • @Charles-olivierDemers:不幸的是,我真的不能这样做,因为一旦您启动应用程序并完成了演练,默认情况下会显示登录页面。如果我这样做了,它会在您每次运行应用程序时导航到设置页面,这不是我想要的:p
  • @lukesIvi: cf 上面的评论 :)

标签: ios swift uiviewcontroller uinavigationcontroller


【解决方案1】:

好的,感谢@Leonardo 告诉我正确的方向!

我通过在 appDelegate 中执行以下操作解决了这个问题:

/*  
*   Override window root view and set it to a newly initialized one
*   view: StoryboardID of view to display
*   navigateTo: if true, set the root view to NavCtrl and then navigate to the desired view
*/
func setWindowViewTo(view: String, navigateTo: Bool) {

    //initalize storyboard & window programmatically
    window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    //if true, navigate from landing page to specified view through navigationController
    if(navigateTo) {

        //instantiate the navigation controller
        let navCtrl = storyboard.instantiateViewControllerWithIdentifier("NavCtrl") as! UINavigationController

        //instantiate the landing page & the page we wish to navigate to
        let landingView = storyboard.instantiateViewControllerWithIdentifier("Main View")
        let vc = storyboard.instantiateViewControllerWithIdentifier(view)

        //manually set the navigation stack to landing view + view to navigate to
        navCtrl.setViewControllers([landingView, vc], animated: false)

        //replace the rootViewController to the navigation controller
        window!.rootViewController = navCtrl

        //make it work
        window!.makeKeyAndVisible()

    } else {
        window!.rootViewController = storyboard.instantiateViewControllerWithIdentifier(view)
        window!.makeKeyAndVisible()
    }
}

重要的一步是在使用 storyboard.instantiateViewControllerWithIdentifier() 方法实例化 NavigationController 时确实强制向下转换 as! UINavigationController

那么就只是一个案例,正确实例化你想要的导航栈的视图,最后调用navCtrl.setViewControllers([view1, view2], animate: false)

感谢大家的帮助!

【讨论】:

    【解决方案2】:

    可以使用- setViewControllers:animated:方法设置UINavigationController的导航栈

    这是reference

    但我不认为这是你的问题,如果我正确地理解了你的代码,它应该是

    //This is the navigation controller
    if let mainView = self.storyboard?.instantiateViewControllerWithIdentifier("NavCtrl"){
    
        //Nav being modally presented
        self.presentViewController(mainView, animated: false, completion: nil)
    
       // Instantiate the settings view
       if let settingsView = self.storyboard?.instantiateViewControllerWithIdentifier("Settings View"){
           //Push it to the navigation controller presented
           mainView.pushViewController(settingsView, animated: false)
       }
       else{
          //Can't Instantiate, deal with error
       } 
    }
    else{
       //Can't Instantiate, deal with error
    }
    

    【讨论】:

    • 不幸的是,这会引发一个错误,指出 mainView 没有名为 pushViewController 的成员(因为它被实例化为 ViewController 而不是 NavigationController...?)
    • 哦,我认为它是 UINavigationController 因为你使用了标识符NavCtrl
    • NavCtrl 确实是 UINavigationController 的情节提要 ID。也许我需要强迫沮丧?
    • 试试if let mainView = self.storyboard?.instantiateViewControllerWithIdentifier("NavCtrl") as? UINavigationController
    猜你喜欢
    • 2022-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多