问题
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