4

Noobie iOS developer with an adopted iOS app here.

I have a settings piece of an iOS app and when the user clicks done, I want the modal view controller (which it currently does) and I want to call a function called updateMainUI in the presentingViewController.

Here's the method I am calling:

- (void)done:(id)sender
{
  [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil]; // works 
  [[self presentingViewController updateMainUI]; //doesn't work

But I get the following error:

No visible @interface for 'UIViewController' declares the selector 'updateMainUI'

But I have declared this in the presenting controller

@interface LocationsViewController : UITableViewController <CLLocationManagerDelegate, UISearchBarDelegate, UISearchDisplayDelegate>
-(void)updateMainUI;

But it's odd that the error should be about UIViewController when the presenting controller is a UITableViewController.

Any ideas on how to get the updateMainUI working? And what I'm doing wrong?

thx

edit #1 This is how I'm creating this - any idea how to get a refernce to the *nearbyViewController?

UIViewController *nearbyViewController = [[LocationsViewController alloc] initWithNearbyLocations];
UINavigationController *nearbyNavigationController = [[UINavigationController alloc] initWithRootViewController:nearbyViewController];
...
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nearbyNavigationController, browseNavigationController, eventsNavigationController, nil];
timpone
  • 19,235
  • 36
  • 121
  • 211
  • This link gives the solutions: http://stackoverflow.com/questions/6203799/dismissmodalviewcontroller-and-pass-data-back – Mathieu Feb 01 '13 at 15:29
  • did you find answer to this? i am having same problem. i have `tabbar` in which i am calling a `navgationcontroller` as a modal view. now the `presentingViewController` property is returning reference to `UITabBar`. – Sam Sep 24 '13 at 13:37

3 Answers3

4

After dismissing ones self, you should expect self.presentingViewController to become nil. Try calling updateMainUI just before dismissing.

Daniel
  • 23,129
  • 12
  • 109
  • 154
  • thx ... hmm... still getting the error. Should I be able to call an instance method the way that I have? – timpone May 12 '12 at 02:50
  • Yes you should be able to, one thing you could try is casting the presentingViewController to your class name. – Daniel May 12 '12 at 02:53
  • if I NSLog the presnetingViewController, it tells me it's a UITabBarController - but the presenting view controller should be a UITableViewController in a UINavigationController in a UITabBarController - I'm probably not understanding how [self presentingViewController] is working. I really just want a reference to the immediate controller. – timpone May 12 '12 at 03:00
  • Apple docs: If the view controller that received this message is presented by another view controller, this property holds the view controller that is presenting it. If the view controller is not presented, but one of its ancestors is being presented, this property holds the view controller presenting the nearest ancestor. If neither the view controller nor any of its ancestors are being presented, this property holds nil. ===> Thus it is not your Table view presenting the modal view controller, but it's first parent that can handle it. – Daniel May 12 '12 at 03:08
  • ok - so I updated with edit #1 above - not fully sure how I'd get navigate this controller references. Any help would be appreciated. – timpone May 12 '12 at 03:28
1

Crud, I see the real answer now. It's a casting problem. Try this:

[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
[(LocationsViewController *)[self presentingViewController] updateMainUI];

You need to be very certain that -presentingViewController returns an object of type LocationsViewController or you will have bigger problems on your hands.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • hmm... but the presentingViewController still comes back as the UITabBarController not as the LocationsViewController. I tried navigationg the viewControllers array but to no avial. I can't believe how convoluted this is :-( – timpone May 12 '12 at 05:05
0

[[self presentingViewController updateMainUI] is broken and won't compile. Assuming you meant [[self presentingViewController] updateMainUI], then there are other ways of doing it.

LocationsViewController *presenting = (LocationsViewController *)[self presentingViewController];
[presenting dismissViewControllerAnimated:YES completion:^{
    [presenting updateMainUI];
}];

Using the variable presenting is an important step.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • yes, I had the other way but edited for ? - will update. Hmm.... so there's definitely something I'm missing about the presentingViewController because I get error when compiling your fix: `Incompatible pointer types initializing 'LocationsViewController *__strong' with an expression of type 'UIViewController *'` – timpone May 12 '12 at 03:08
  • OK, as long as you are *very* sure of the type, cast it `LocationsViewController *presenting = (LocationsViewController *)[self presentingViewController];` – Jeffery Thomas May 12 '12 at 04:34
  • the issue is that presentingViewController != the view controller that instantiated it. ughh... – timpone May 12 '12 at 05:14