3

I am working on an application where I need to pass an array of values from a click of tableview cell in my tableviewcontroller which is the initial view controller to a contentview view controller with page curl transition.

Each of the page will have a textview which will be populated with value passed from the initial view controller and can be edited by the user.

My issue is, I am not able to update the array with the new value and pass it back to the initial view controller

I tried the following:

  1. I implemented pageviewcontroller methods in my initialview controller to create instances of the ContentViewController with page curl transition and was able to pass values to each of the pages when the page curl was done. But I am trying to figure out a way to pass back the updated value to the tableviewcontroller from where I instantiated the object.

  2. I tried Segue from tableview to the contentViewcontroller, but it does not work.

Appreciate if somebody can help me.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Raghu
  • 49
  • 4
  • I recently gave quite a detailed account on two-way interaction between view controllers (specifically from `UITableViewController` to `UIViewController`), using methods and actions associated with segues. You should be able to adapt the method I describe there to your case. See http://stackoverflow.com/questions/34247239/global-variable-and-optional-binding-in-swift/34275632#34275632 – dfrib Dec 15 '15 at 17:37

1 Answers1

1

Use the delegate pattern.

Define a protocol within ContentViewController :

protocol UpdateModelDelegate {
    func didDismissContentViewController(controller:ContentViewController)
}

Establish a delegate variable within ContentViewController :

var delegate: UpdateModelDelegate?

When you dismiss ContentViewController call delegate?.didDismissContentViewController(self) which will send the data back to your UITableViewController.

Have the UITableViewController conform to this protocol.

class MyTableViewControllerSubclass: UITableviewController, UpdateModelDelegate

When presenting the ContentViewController set your UITableViewSubclass as the delegate.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var contentViewController: ContentViewController()
    //After you pass the necessary data to contentViewController...
    contentViewController.delegate? = self
}

finally, implement the delegate function within your UITableviewController subclass.

func didDismissContentViewController(controller:ContentViewController) {
    //You're passed in the contentViewController here, use relevant data to update your model. 
}
BenJammin
  • 1,479
  • 13
  • 18
  • ok. Assuming I implement the protocol method that will pass me the contentviewcontroller object back, I think I will face a problem here.as I mentioned, I am instantiating the contentviewconntroller from the tableviewcontroller as under, each time the user does a page curl: – Raghu Dec 16 '15 at 07:01
  • I have explained a bit in edit section. to summarize, I would like to know if a page curl transition performed by user will trigger dismissal of contentviewcontroller and pass the value to the tableviewcontroller, in which case I will be able to update the array element with the current value,. Next, how do I dismiss contenviewcontroller and go back to tableviewcontroller, as I do not have a segue established. – Raghu Dec 16 '15 at 08:53
  • How are you presenting the ContentViewController? When the user selects a cell in your tableview, you should be presenting a new instance of ContentViewController. Link an "exit" button up via interface builder, connect it to an action in ContentViewController, and call your delegate method there. Also call self.dismissViewController(animated) to dismiss the ContentViewController – BenJammin Dec 18 '15 at 17:53
  • I present contentviewcontroller as an instance created from the tableview controller like this :let storyBoard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) let dataViewController = storyBoard.instantiateViewControllerWithIdentifier("contentView") as ContentViewController. So every page curl will create a new instance . I tried calling the delegate method suggested by you, but it is throwing an error :[PageApp.ContentViewController goBack:]: unrecognized selector sent to instance 0x7fa3d9574c00 2015-12-20 20:35:22.818 PageApp[1679:111678] *** Terminating app – Raghu Dec 20 '15 at 12:41
  • providing more details on the error:2015-12-20 20:42:54.333 PageApp[1716:113713] -[PageApp.ContentViewController goBack:]: unrecognized selector sent to instance 0x7ff68a488c90 2015-12-20 20:42:54.337 PageApp[1716:113713] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PageApp.ContentViewController goBack:]: unrecognized selector sent to instance 0x7ff68a488c90' *** First throw call stack: ( – Raghu Dec 20 '15 at 12:44
  • I changed the button attribute to action and hooked it to closeContentView like this, inside ContentViewController: @IBAction func closeContentView(sender:UIButton!) { self.delegate?.closeContentViewController(self) } Inside TableViewController I call the delegate method as under: func closeContentViewController(controller: ContentViewController) { NSLog("Inside delegate method closeContentViewController.\n") self.dismissViewControllerAnimated(true , completion: nil) } BUT the transition does not happen. I see the log message above inside tableviewcontroller – Raghu Dec 20 '15 at 14:20