1

I am looking to perform two different segues.

I have a VC that is entered by "Present Modally". From that VC I have another segue(Push) leading to a TableView VC.

Problem 1

I want to be able to go back from the table view to the previous VC once you touch a row. So my problem here is that I don't know what type of segue to use in order to go back one step but also sending data at the same time.

I have a close button set as "unwind to VC" but that is just closing my VC.

Problem 2

From the TableView I have another segue(push) leading to another TableView VC. - You pick category and then sub-category. My problem here is that I don't know how to go back one step OR how to make a segue to VC1 that sends the info from both table view controllers.

Thanks

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
Kiwo Tew
  • 1,491
  • 1
  • 22
  • 39
  • Take a look at this post, it may help: http://stackoverflow.com/questions/12569316/does-anyone-know-what-the-new-exit-icon-is-used-for-when-editing-storyboards-usi – Aaron Rasmussen Aug 19 '15 at 15:30

1 Answers1

5

Problem 1:

On top of the YourTableViewController class (not inside the class but on top meaning outside) implement the following protocol:

protocol MyTableViewControllerDelegate {
func tableViewController(controller: YourTableViewController, didFinishPicking Item item: SomeItemYouWantToPassBack)
}

Then you do the following: In the your table View controller class(YourTableViewController) implement these:

weak var delegate: MyTableViewControllerDelegate? \the style to implement delegates.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let itemYouWantToPassBackToVC: SomeItemYouWantToPassBack = dataImplementedInTableView[indexPath.row]
delegate?.tableViewController(self, didFinishPickingItem item: itemYouWantToPassBackToVC)

dismissViewControllerAnimated(true, completion: nil)


tableView.deselectRowAtIndexPath(indexPath, animated: true)


}

then in your previous VC you implement the function you created in the protocol:

func tableViewController(controller: YourTableViewController, didFinishPicking Item item: SomeItemYouWantToPassBack) {

// Here you take the "item" parameter and use it for your purpose. It is the item you wanted to pass back here

}

In addition read the apple documentation to learn more about the protocols and delegates.

Problem 2:

OK... in this case I would suggest the following: implement the following function in VC1:

@IBAction func unwindToVC1() {
\\you can leave this place empty 
}

Then, pay attention to these words: In your storyBoard find view controller for TableView number 2 (The one showing all subcategories) and on top of it you will see three buttons. CTRL + drag from yellow to the Red exit door and choose "unwindToVC1" from the popup.

In the document Outline (the list view displaying all you have in the storyBoard) locate the newly created segue and give a name to it, let us say "segueVC1".

Then in TableViewControllerNumberTwo implement this:

override func tableView(tableView: UITableView, didSelectItemAtIndexpath indexPath: NSIndexPath) {

    let item1 = yourDataModel[indexPath.row]
    performSegueWithIdentifier("segueVC1", sender: item1)

}

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

   if let segue.identifier == "segueVC1" {
   let vc = segue.destinationViewController as VC1 \\ VC1 is the first vc you want to segue to
   vc.modelToReceive = sender as modelToReceiveClass

 }
}
IamMashed
  • 1,811
  • 2
  • 21
  • 32