0

我在堆棧溢出社區中閱讀了很多有關在不同ViewController之間切換的問題/答案,但找不到正確的答案來解決我的特定問題。在SWIFT中的不同視圖之間切換

  1. 我使用NavigationViewController「連接」我的所有不同viewControllers(視圖控制器/ TableViewController/CollectionViewController)

  2. 我有這些不依賴於一個視圖控制器類的一些類。例如以計算我的「計算」類中的任何內容。

我正在尋找一個簡單的解決方案,以在所有ViewController之間切換。我想在我的「計算」類中做出決定,例如在我的Navigation ViewController中退一步。

在目前我使用這樣的:

  1. 實施例在兩個不同ViewControllers之間切換:

    let sb = UIStoryboard(name: "Main", bundle: nil) 
    let vc = sb.instantiateViewControllerWithIdentifier("StartPage") as! StartPageView 
    self.presentViewController(vc, animated: true, completion: nil) 
    

問題:此示例僅適用於一個的ViewController類和導航酒吧消失。

  • 例回去在我NavigationViewController一個步驟:

    self.navigationController?.popViewControllerAnimated(true) 
    
  • 問題:此示例僅適用於一個的ViewController類。

    編輯1:

    我找到了一個很好的解決方案打電話給我所有viewControllers與這部分代碼:

    let switchViewController = self.navigationController?.viewControllers[1] as! ScanTableView 
        self.navigationController?.popToViewController(switchViewController, animated: true) 
    

    我怎樣才能把這個代碼在非視圖控制器類?

    回答

    1

    推新視圖控制器到您現有的導航堆棧,而不是呈現其模態:

    let vc = self.storyboard!.instantiateViewControllerWithIdentifier("StartPage") as! StartPageView 
    self.navigationController!.pushViewController(vc, animated: true) 
    
    +0

    此代碼工作得很好,切換從一個視圖控制器嘍!現在導航欄不會消失。但是我不能在另一個類中使用此代碼,而不是ViewController類。我收到錯誤消息:「類型TEST_CLASS的值沒有成員'storyboard'」有沒有可能修復這個錯誤?謝謝! – Passe

    +0

    而你的核心還有另一個問題。當我調用它時,兩個ViewController之間的切換運行良好,但是創建了一個新的後退按鈕。有沒有可能阻止這種情況?我想恢復舊的。 – Passe