views:

654

answers:

3

So I have a popover with a button in it. When that button is pushed, I want the popover to go away. It seems easy enough, but I can't figure it out for the life of me. The code I use to show the popover is below:

    AddCategoryViewController* content = [[AddCategoryViewController alloc] init];
 UIPopoverController* aPopover = [[UIPopoverController alloc]
          initWithContentViewController:content];
 aPopover.delegate = self;
 [content release];

 // Store the popover in a custom property for later use.
 self.addCategoryPopover = aPopover;
 [aPopover release];
 [addCategoryPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

Within the addcategoryviewcontroller, I have:

    -(IBAction)saveAddCategory:(id)sender {
 if (rootViewController == nil)
  rootViewController = [[RootViewController alloc] init];

 [rootViewController.addCategoryPopover dismissPopoverAnimated:YES];
 [rootViewController dismissPopover];
}

Rootviewcontroller is where the popover is being created from. Unfortunately, neither of those methods work to dismiss it. any help?

A: 

I have

- (void)viewWillDisappear:(BOOL)anAnimated
{
    [self.dPopover dismissPopoverAnimated: NO];
    self.dPopover = nil;
    [super viewWillDisappear: anAnimated];
}

and don't see why this wouldn't work in your case.

Your "if" is a bit troubling, so my guess is you aren't talking to the view you think you are. rootViewController.addCategoryPopover is probably nil, because you made a new controller.

David Dunham
I made that "if" statement pretty much out of desperation as nothing was working for me. I did at one point just try having [rootViewController.addCategoryPopover dismissPopoverAnimated:YES];as the code in that function. That, however, wasn't working either.
joshholat
You didn't address my implied question — what is the value of rootViewController.addCategoryPopover?
David Dunham
+2  A: 

You would seening a warning at this line.

 aPopover.delegate = self;

and if you would execute your code. The app would crash.

Instead you need to doi like this

Hope this helps.

Thanks,

Madhup

Madhup
A: 

I think I answered just a similar question with the solution I used to dismiss a popover with a UIView loaded from a MKMapView. The use of my solution is basicly the same as for any other view loading a popover.

Have a look at: http://stackoverflow.com/questions/2639350/how-to-dismisspopoveranimated-on-ipad-with-uipopovercontroller-in-mkmapview-sdk3/2799186#2799186

I hope that solved your problem.

Regards, Paul Peelen

Paul Peelen