How to dismiss the UIPopoverController?
Solution 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];
}
Solution 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];
Nazik
Coding is my Passion Coding is my Splendor Coding is my Gixxer ....................................................................................................................................
Updated on June 05, 2022Comments
-
Nazik almost 2 years
I have created a
UIPopoverController
and added it to a view controller when clicking anUIButton
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 theUIButton
in theviewcontroller
,popoverController
is theUIPopoverController
.The
popovercontroller
appears fine while clicking the button.But it won't get dismissed when I click the
UIButton
in thesecondviewcontroller
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 thepopovercontroller
? -
Midhun MP over 11 yearsit's not the case here, he is initializing new instance for dismissing the old popover
-
Nazik over 11 years[controller setFs:self], it gives an error, How to pass the old instance when displaying the popover
-
Midhun MP over 11 years@NAZIK: still have issues ?
-
Nazik over 11 years@ Midhun MP, yes,error - popovercontroller not found on object of type 'PopOverViewController *'- what to do now?
-
Midhun MP over 11 years@NAZIK: please check your code once again, I think you made a mistake. Check whether you are passing [... setFS:self] and check the typr of fs, it should be
Firstviewcontroller
-
Midhun MP over 11 yearsdeclare the fs in your SecondViewController class's @interface and create a property
@property (nonatomic, assign) Firstviewcontroller *fs;
if you are using non-arc and synthesize it -
Midhun MP over 11 years@NAZIK: my mistake, please check above comment. Type of fs is
Firstviewcontroller
-
Nazik over 11 yearsU said " Check whether you are passing [... setFS:self] and check the typr of fs" where to write this and what is 'setFS'
-
Nazik over 11 yearsWhen writing [controller setFS:self]; it gives an error- No visible @interface for 'PopoverViewController' declares the selector 'setFS:'
-
Midhun MP over 11 yearsyour
controller
is of typeSecondViewController
is it ? -
Midhun MP over 11 yearsI told you to put
@property (nonatomic, assign) Firstviewcontroller *fs;
this code on yourSecondViewController
's@interface
. Did you do that ? -
Nazik over 11 yearsyes I did it, and [fs.popoverController dismissPopoverAnimated:TRUE]; throws no error but it didnot dissmiss the popover
-
Nazik over 11 yearswhat is [controller setFs:self]
-
Midhun MP over 11 yearscontroller is the instance of your
SecondViewController
and you are setting the firstviewcontroller instance to fs using the setter method -
Midhun MP over 11 years@NAZIK: Ok, please share that
-
Nazik over 10 yearsHi Midhun, take a look at my question, pl answer if you can.
-
Midhun MP over 10 years@NAZIK: I added a comment in your question, please check. It'll work. I think you already got that answer
-
Nazik over 10 yearsThanks for info, I got the solution.