0
votes

I have several popovers in my application and I am having difficulty in determining which popover was dismissed. Is there a "tag" feature equivalent for UIPopOvers?

I can NSLog the popoverController in the popoverContorllerDidDismissPopover method and see the memory reference of each one but that doesn't help.

#pragma mark - Popover controller delegates
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    NSLog(@"Popover dismised %@", popoverController);
}
3
Maybe you could use UIPopOverController with blocks? This way you wouldn't have to care about that.Rui Peres
What are you trying to acheive?Mike D
My goal is that when a specific popover is dismissed, I can update a couple of different button states. But not until after the popover is dismissed. Yes, I could continuously up date the buttons state based up whatever is changed, but that isn't my requirement.JasonBourne
I think in that case you might want to look into using a delegate.Mike D

3 Answers

1
votes

An extract from here:

If I understand the question, then basically, no - and it's maddening. On the one hand you're told that only one popover should be showing at any one moment. On the other hand you don't automatically get a reference to that popover. Thus it is up to you to store a reference, manually, to the current popover controller at the time it shows its popover, so that you can talk to it later in order to dismiss it. Popover controller management can thus get really elaborate and clumsy; you're doing all kinds of work that the system should just be doing for you.

iOS is funny this way. I'm reminded of how there's no call in iOS 4 that tells you current first responder. Obviously the system knows what the first responder is, so why won't it tell you? It's kind of dumb. This is similar; the system clearly knows useful stuff it won't share with you. m.

0
votes

There are many ways how to distinguish between popovers. I will list few of them:

  1. You are asking about tag. Note that every popover has a content view controller and this controller has a view that can be tagged. However, using magic integer tags to distinguish between views is arguable in general.

  2. Store the type of the popover into a variable/property in your controller, e.g. as an enum. This is the simplest way.

  3. Add the neccessary information to the popover, but be clever about it, e.g.

@interface MyPopoverController : UIPopoverController 

@property (nonatomic, copy, readwrite) void (^dissmissHandler)(void);

@end

@implementation MyPopoverController 

- (id)initWithContentViewController:(UIViewController*)contentView {
   self = [super initWithContentViewController:contentView];

   if (!self) {
      return nil;
   }

   self.delegate = self;

   return self;
}

- (void)popoverControllerDidDismissPopover:(UIPopoverController*)popover {
    assert(popover == self);

    if (self.dissmissHandler) {
       self.dissmissHandler();
    }
}

@end

MyPopoverController* popover = [MyPopoverController alloc] initWithContentViewController:...];
popover.dissmissHandler = ^{
   ...
};

0
votes

As @Anoop stated, you can usually only have one popover showing at a time.

One possible solution is to check the contentViewController property on the pop over. If you are storing a reference of each view controller you could do something like:

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    if ( popoverController.contentViewController == self.someUIViewController ) {
        // do stuff
    }
    else if ( popoverController.contentViewController == someoTherViewController ) {
       //
    }
    NSLog(@"Popover dismised %@", popoverController);
}

If storing a reference to each content view controller is not possible (or maybe just not a good idea), you could always check its type:

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    if ( [popoverController.contentViewController isKindOfClass:[MyAwesomeViewController class]] ) {
        // do stuff
    }
    else if ( [popoverController.contentViewController isKindOfClass:[MyOtherViewController class]] ) {
        //
    }
    NSLog(@"Popover dismised %@", popoverController);
}

Another possible solution, which is probably better from a design stand point of view, would be to pass in a delegate to the view controller contained in the pop over. More here. This way, the view controller displayed can send data back to your main view controller.