want to call a method in the presentingViewController after user has clicked done

我的梦境 提交于 2019-12-22 09:37:41

问题


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

回答1:


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




回答2:


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:


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



来源:https://stackoverflow.com/questions/10560636/want-to-call-a-method-in-the-presentingviewcontroller-after-user-has-clicked-don

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!