问题
I have created a UIPopoverController and added it to a view controller when clicking an UIButton as follows
- (void)viewDidLoad
{
[super viewDidLoad];
controller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
popoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
}
- (IBAction)showPopover:(UIButton *)sender
{
if ([popoverController isPopoverVisible]) {
[popoverController dismissPopoverAnimated:YES];
} else {
CGRect popRect = CGRectMake(self.btnShowPopover.frame.origin.x,
self.btnShowPopover.frame.origin.y,
self.btnShowPopover.frame.size.width,
self.btnShowPopover.frame.size.height);
[popoverController presentPopoverFromRect:popRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
}
btnShowPopover is the UIButton in the viewcontroller, popoverController is the UIPopoverController.
The popovercontroller appears fine while clicking the button.
But it won't get dismissed when I click the UIButton in the secondviewcontroller
I used the following code for that
-(IBAction)y:(id)sender{
fs = [[Firstviewcontroller alloc] initWithNibName:@"FIrstscreen" bundle:[NSBundle mainBundle]];
[fs.popoverController dismissPopoverAnimated:TRUE];
}
But it didn't work.
How to dismiss the popovercontroller when clicking the button in a viewcontoller that was added to the popovercontroller?
回答1:
You are allocating a new instance of Firstviewcontroller, so it won't dismiss the previous instance's popover.
You need to pass the old instance when you are displaying the popover like:
- (void)viewDidLoad
{
[super viewDidLoad];
controller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
popoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
[controller setFs:self]
}
And dismiss like:
-(IBAction)y:(id)sender
{
[fs.popoverController dismissPopoverAnimated:TRUE];
}
回答2:
Apple docs:
The popover controller does not call this method in response to programmatic calls to the
dismissPopoverAnimated:method. If you dismiss the popover programmatically, you should perform any cleanup actions immediately after calling the dismissPopoverAnimated: method.
So the didDimiss delegate's method won't be called by itself.
Try:
[self.popover dismissPopoverAnimated:YES];
[self.popover.delegate popoverControllerDidDismissPopover:self.PopUp];
来源:https://stackoverflow.com/questions/14317252/how-to-dismiss-the-uipopovercontroller