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
}
}