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?
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With