want to call a method in the presentingViewControl

2019-04-10 08:10发布

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];

3条回答
爷、活的狠高调
2楼-- · 2019-04-10 08:31

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.

查看更多
仙女界的扛把子
3楼-- · 2019-04-10 08:35

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

查看更多
来,给爷笑一个
4楼-- · 2019-04-10 08:40

[[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.

查看更多
登录 后发表回答