2016-01-08 35 views
1

我目前正在編寫一個應用程序,我們在用戶第一次打開該應用程序時啓動漫遊。最後,我們要求用戶填寫一些細節,爲此我希望他按下按鈕以重定向到設置頁面。實例化視圖,然後導航

問題是,此頁面位於導航控制器的下一級(從着陸頁開始)。就目前而言,我可以正確實例化着陸頁,但重定向到設置頁面從不發生。

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故事板來得及instiate第一或第二視圖之前。

所以,我有幾個問題:

  • 有沒有一種辦法,實際上,創建一個視圖,然後導航到另外一個已經被實例化後,對不對? (這是爲了保持導航堆棧並保持準確的導航欄行爲)
  • 如果沒有,是否有可能以編程方式填充導航堆棧,以便我只需要實例化設置視圖?這是爲了在導航欄中仍然有後退按鈕可以發送到登陸屏幕?

在此先感謝球員

+0

當你做'self.navigationController?.pushViewController(settingsView!,animated:false)'你確定'self.navigationController?'不是零?主視圖控制器不在導航堆棧上呈現,正在以模態呈現 – Leonardo

+0

在您的第一個視圖控制器的viewDidLoad中,您可以將self.performsegue ...添加到您正在執行兩次的其他視圖 –

+0

。 1ce'presentViewController',然後'self.navigationController?.pushViewController'。正如你所提到的,下面的兩行永遠不會被執行。你所要做的就是在你最初推送的navigationController的'viewDidLoad'中設置一個函數,該函數實例化爲你希望推送到的另一個VC。並且要有後退按鈕,將導航嵌入到NavigationController中。 – lukaivicev

回答

2

感謝@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)

謝謝大家的幫助!

1

可以使用- 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 
} 
+0

不幸的是,這會拋出一個錯誤,指出mainView沒有一個名爲'pushViewController'的成員(因爲它被實例化爲ViewController而不是NavigationController ...?) – Skwiggs

+0

噢,我把它當成了一個UINavigationController,因爲你使用的標識符' NavCtrl' – Leonardo

+0

NavCtrl的確是一個UINavigationController的故事板ID。也許我需要強制沮喪? – Skwiggs