Domanda

I have a button in my popover controller. I want to use it to dismiss the popover, so I am trying to access a method (dismissPopover) of the presenting view controller (the "root" view controller).

Note: the method to dismiss the popover is already set up and working, in the root VC, which is the delegate. If I call it it will dismiss the popover. I just need to access the method from the popover.

To do this I set up a property in the AppDelegate, and get an instance of the rootVC like this: self.rootController = (ViewController*)self.window.rootViewController;. Then I imported the root VC class and the AppDelegate to the popover's view controller's class, as below. Seems to give me access to the rootVC, and the methods, but the results do not fire the method. Any idea what I am missing here?

#import "ViewController.h"
#import "AppDelegate.h"

Action connected to button:

- (IBAction)dismissPopover:(id)sender {

//Checking the button works, it does:
NSLog(@"dismissPopover, from popover");

//Trying to get an instance of the rootViewController, the "presenting view controller"                                  
ViewController *rootVC = [(AppDelegate *)[[UIApplication sharedApplication] delegate] rootController];

//trying to access the method in the rootVC that dismisses the popover
[rootVC dismissPopover];

//Tried the following code, does nothing:
//[self dismissPopoverAnimated:YES];
}

NOTE: I ended up abandoning the use of a popover for this as it became a bit over complicated. I tried loading my view controller into a UIView (so I could load the contents of a nib to a pop-up view). That also became a bit complicated. So, for now I am just building my desired interface in a UIView programatically. So far works great.

È stato utile?

Soluzione

  1. dismissPopoverAnimated: is a method of UIPopoverController class. so, you need a popover controller reference in your 'root' view controller.

    MyRootViewController.myPopoverController = thePopover;
    
  2. the button is in your 'root' view controller, and in it's action method:

    [self.myPopoverController dismissPopoverAnimated:YES];
    

Altri suggerimenti

In iOS 8, you can dismiss the popover (if it's coming from a segue, at least) with dismissViewControllerAnimated:completion: from within the popover. Doesn't work in iOS 7 (or below), however.

Popover automatically dismissed when clicking outside it , as you order a button to dismiss it you can simply use the following code inside your dismissPopover method :

         [self.popoverController dismissPopoverAnimated:YES];

you don't need all this tedious work !

[self dismissViewControllerAnimated:YES completion:nil];

is the solution; you just need an IBoutlet or add target to your button and then call above line

I had the same problem

just do in your buttonClickMethod:

[yourPopoverController dismissPopoverAnimated:YES];

hope you help!

cheers

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top