2015-04-21 57 views
2

如何通過UINavigationController從UITabBarController子ViewController執行Segue到具有UINavigationController的UITableViewController的DetailView?在多個控制器上執行segue

enter image description here

有兩個孩子的,的firstView(最多查看符號)和NavigationController(聯繫人符號)一個TabBarController。

FirstView有一個按鈕,它應該執行一個循環到Show Profile VC。 NavCont有一個TableView,子類爲All ProfilesTVC,原型單元爲子類。 AllProfilesTVC有一個由重用單元格顯示的三個名稱的數組。

哪一個viewcontroller我必須在FirstView(HomeVC)函數prepareForSegue中進行實例化和準備,以及在故事板中創建的segue直接指向哪裏?所以我在「John's」DetailView。 當我向ShowProfileVC執行segue時,我可以將後退按鈕返回到所有ProfilesTVC嗎?

還有誰想要在github repo

的firstView/HomeVC.swift

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

    // what do we do here ??? 
} 

AllItemsTVC

class AllItemsTVC: UITableViewController { 

let profiles = ["Joe", "John", "Ken"] 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return profiles.count 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell 
    let profile = profiles[indexPath.row] as String 
    cell.textLabel?.text = profile 
    return cell 
} 

@IBAction func cancelFromShowProfile(segue: UIStoryboardSegue) { 
} 

// MARK: - Navigation 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

    if let identifier = segue.identifier { 
     switch identifier { 
     case "ShowProfile": 
      if let showProfileVC = segue.destinationViewController as? ShowProfileVC { 
       // pass the data to the destinationVC 
       let selectedProfile = profiles[tableView.indexPathForSelectedRow()!.row] as String 
       showProfileVC.name = selectedProfile 
      } 
     default: break 
     } 
    } 
} 

嘗試ShowProfileVC

class ShowProfileVC: UIViewController { 

@IBOutlet weak var textLabel: UILabel! { 
    didSet { 
     textLabel.text = name 
    } 
} 

var name = "Label" 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 
01 GitHub庫

}

感謝您的任何幫助。

回答

0

你不能用segue做到這一點,但這不是問題,因爲你可以在代碼中簡單地做到這一點。只要給你的視圖控制器一個identifier故事板,並通過此標識符與適當的UIStoryboard API實例化它們。

首先切換到代碼中的其他標籤欄項目,然後首先告訴導航控制器popToRootViewController,之後您可以將所有必要的控制器輪流推入導航堆棧。在推動控制器之前,您可以在prepareForSegue中執行通常所做的所有配置。

訣竅是將animated設置爲false,除了最後一步。

相關問題