1

When you create a modal View Controller using code, the modal controller has the ability to access the presenting controller.

Please read following. This is the crux of question. I do not want to use delegates.

According to Apple docs:

When you present a view controller modally (either explicitly or implicitly) using the present(_:animated:completion:) method, the view controller that was presented has this property set to the view controller that presented it.

I want to set the value of a property of the presenting view controller. However, I don't create the VC in code. I use a segue from a button.

This gives error: Property afterAdd not found on object of type UIViewController:

   self.presentingViewController.afterAdd = YES;

where the property afterAdd is set as follows:

@property (nonatomic) BOOL afterAdd;

Is there any way to set the value of a property in the presenting view controller when the link is by storyboard that is analogous to the above and does not involve using delegate methods?

user6631314
  • 1,751
  • 1
  • 13
  • 44
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – dive Nov 19 '16 at 01:22
  • Those answers ignore presentingViewController which is the essence of my question. – user6631314 Nov 19 '16 at 01:24
  • I understand. But the mentioned post describes the best practices. Your code relates to `presentingViewController` which can be different after refactoring or when you change your navigation flow. I recommend you to use one the technique described in the post marked as duplicate. Anyway, I post the answer for your problem below. – dive Nov 19 '16 at 01:35

1 Answers1

1

You should cast self.presentingViewController to your real view controlller class. Something like this, just replace MyCustomViewController with your real controller name: ((MyCustomViewController *)self.presentingViewController).afterAdd = YES;.

dive
  • 2,130
  • 1
  • 17
  • 26
  • Also, if you use `segue` for presentation, you can setup your controller in `prepareForSegue:sender` function. More information can be found there [Using Segues](https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html). – dive Nov 19 '16 at 01:31
  • This makes sense but is giving error "Expected Expression". – user6631314 Nov 19 '16 at 16:44
  • Never mind, I had typo. Extraneous character. This worked. Thanks. – user6631314 Nov 19 '16 at 16:51
  • There was a typo. But after fixing it, it tries to find the property on the TabViewController in which the presenting controller is embedded. – user6631314 Nov 19 '16 at 16:57
  • Try to use [.selectedViewController](https://developer.apple.com/reference/uikit/uitabbarcontroller/1621172-selectedviewcontroller) on your tab bar, like `((MyCustomViewController *)((UITabBarController *)self.presentingViewController)).afterAdd = YES;`, but as I told you early - this is the bad practice, I'm sure there will be more problems. I still recommend you to try `prepareForSegue:sender` technique. – dive Nov 20 '16 at 13:04
  • I know how to use prepareForSegue to pass data forward. But I don't know how to use it to get the data back from the desintationViewController. Basically when the destinationVC is ready to dismiss itself after doing a save, I want it to change a value back in the presentingView Controller to prevent something from happening. The delegate approach just seems very complicated and therefore fragile. – user6631314 Nov 20 '16 at 17:24
  • How would I add selectedVC to the mix? ((myVC *)((UITabBarController *)self.presentingViewController.selectedViewController.afterAdd does not work. Alternatively, could I do something with myVC * rootViewController = [self.navigationController.viewControllers firstObject]; – user6631314 Nov 20 '16 at 17:41
  • The idea is to retrieve your view controller from hierarchy and call the method you want, but I cannot help in this situation, because I need to know your's view hierarchy to propose a valid option. If you want to pass data back, you should use unwind segues ([Using Unwind Segues](https://developer.apple.com/library/content/technotes/tn2298/_index.html)). With unwind segue you can handle dismissing of your destination view controller in presenting view controller. – dive Nov 22 '16 at 19:19