2016-07-31 65 views
1

我想在我的斯威夫特應用程序來創建以下用戶流量:如何回彈到不同導航控制器中的視圖?

  1. 用戶點擊一個按鈕
  2. 模態的視圖彈出了一個選擇。用戶選擇一個選項。
  3. 第二個視圖以另一種選擇出現(使用「顯示」轉換,因此用戶可以按<  返回以更改選擇#1)。用戶選擇第二選擇。
  4. 第二個模態視圖回退到原始視圖(其中有按鈕)。通過傳遞到VC1參考一路走過來VC3,然後調用

    --> NC1 --> VC1 --modal-> NC2 --> VC2 --show-> VC3 (NC = UINavigationController, 
          ^        |  VC = UIViewController) 
          |         | 
          ----Segue I'm trying to achieve----- 
    

    我一直在努力,一直流行回VC1:

要做到這一點,我創建了以下結構VC3中的以下代碼:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    // Perform Segue 
    print("Trying to segue") 
    rootController!.navigationController!.popToRootViewControllerAnimated(true) 

    tableView.deselectRowAtIndexPath(indexPath, animated: true) 
} 

其中rootController是對VC1的引用。不幸的是,賽格不會發生。第二個模態視圖停留在屏幕上,但我得到了「嘗試繼續」消息。

我試圖以正確的方式實現這個用戶流?如果是這樣,任何建議,爲什麼我不能彈出到另一個導航控制器的根視圖?

+0

有包含在你駕駛的所有控制器的屬性稱爲viewControllers,刪除那些你想跳過 –

回答

3

如果您正在使用故事板,則可以使用展開的漸變來完成此操作。

在VC1實施@IBAction方法,它接受一個UIStoryboardSegue作爲參數:

@IBAction func unwindToVC1(sender: UIStoryboardSegue) { 

} 

然後從VC3控制拖動到視圖控制器的退出圖標的原型細胞,選擇Selection Segue: unwindToVC1:,你是好去。

enter image description here

可以實現使用代碼也是類似的結果。
在VC3實現didSelectRowAtIndexPath這樣的:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    navigationController!.dismissViewControllerAnimated(true, completion: nil) 
} 
在您的導航控制器
+1

感謝尤,我用'dismissViewControllerAnimated '正如你在VC3中所建議的那樣,它完成了這項工作。 –

相關問題