如何解雇UIPopoverController? [英] How to dismiss the UIPopoverController?
问题描述
我创建了一个 UIPopoverController
,并在点击 UIButton
时将其添加到视图控制器中,如下所示
- (void)viewDidLoad
{
[super viewDidLoad];
controller = [[SecondViewController alloc] initWithNibName:@SecondViewControllerbundle:[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
allowedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
}
btnShowPopover
是 viewcontroller
中的 UIButton
, popoverController
是 UIPopoverController
。
单击按钮时, popovercontroller
显示正常。
<但是当我点击
secondviewcontroller
$ b $中的 UIButton
时,它不会被解雇b 我使用了以下代码
- (IBAction)y:(id)sender {
fs = [[Firstviewcontroller alloc] initWithNibName:@FIrstscreenbundle:[NSBundle mainBundle]];
[fs.popoverController dismissPopoverAnimated:TRUE];
}
但它不起作用。
单击添加到 popovercontroller
的viewcontoller中的按钮时,如何关闭 popovercontroller
?
您正在分配一个新的 Firstviewcontroller
实例,所以它不会忽略前一个实例的popover。
当您显示popover时需要传递旧实例:
- (void)viewDidLoad
{
[super viewDidLoad];
controller = [[SecondViewController alloc] initWithNibName:@SecondViewControllerbundle:[NSBundle mainBundle]];
popoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
[控制器setFs:self]
}
并解雇如:
- (IBAction)y:(id)sender
{
[fs.popoverController dismissPopoverAnimated:TRUE];
}
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];
}
这篇关于如何解雇UIPopoverController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!