当前位置:  开发笔记 > IOS > 正文

在BarButtonItem打开时按下它时解除iPad UIPopoverController

如何解决《在BarButtonItem打开时按下它时解除iPadUIPopoverController》经验,为你挑选了2个好方法。

在iPad上使用拆分视图,我有以下代码:

- (void) splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc {
  barButtonItem.title = @"Categories";
  NSMutableArray *items = [[toolbar items] mutableCopy];
  [items insertObject:barButtonItem atIndex:0];
  [toolbar setItems:items animated:YES];
  [items release];
  self.popoverController = pc;
}

这可以很好地显示按下按钮时的弹出窗口.但是,如果按下按钮,我也希望让按钮解除,因为它已经打开以遵循良好的指导方针.我该怎么做呢?(即如果用户反复点击此按钮,则弹出窗口应该会隐藏其他所有击中.)



1> sunnycmf..:

当splitViewController显示popover时,将调用下面的方法.只检查是否为零,然后解雇它:)

- (void)splitViewController:(UISplitViewController*)svc popoverController:(UIPopoverController*)pc willPresentViewController:(UIViewController *)aViewController{
  if ([pc isPopoverVisible]) {
     [pc dismissPopoverAnimated:YES];
  }
}


您应该使用popover属性isPopoverVisible来检查而不是检查是否为nil

2> Justin..:

Apple的HIG说在弹出框中不应该有一个明确的解雇按钮,但要做你要求的,你有两个选择.

1)发布NSNotification

要么

2)向下钻取到视图层次结构,直到有弹出窗口实例

1)在viewDidLoad方法中,您在弹出窗口的任何视图中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissThePopover) name:@"popoverShouldDismiss" object:nil];

创建一个名为"dismissThePopover"的方法,并在dealloc方法中删除Observer

-(void)dismissThePopover {
    [self.popoverController dismissPopoverAnimated:YES];
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

在popoverController"dismiss"按钮中,输入以下行:

[[NSNotificationCenter defaultCenter] postNotificationName:@"popoverShouldDismiss" object:nil];

这样做会向应用程序发送通知,并且由于您已经注册了其他视图控制器来监听它,因此每当它看到该通知时,它会调用您指定的选择器,在这种情况下,dismissThePopover.

2)深入查看视图层次结构以查找self.popoverController

看看这个,肯定会有你的不同,但总的想法是一样的.从你的AppDelegate开始,进入第一个viewcontroller,进入子视图,直到你到达self.popoverController对象.

MyAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
//appDelegate instance, in this case it's the .m file for your ApplicationDelegate

UISplitViewController *svc = appDelegate.splitViewController;
//In this case the first view inside the appDelegate is a SplitView, svc

UINavigationController *navc = [[svc viewControllers]objectAtIndex:0];
//a navigationController is at index:0 in the SplitView hierarchy. DetailView is at index:1

NSArray *vcs = [navc viewControllers];
//vcs is the array of different viewcontrollers inside the Navigation stack for nvc

iPadRootViewController *rootView = [vcs objectAtIndex:0];
//declare the rootView, which is the .m file that is at index:0 of the view array

UIPopoverController *pc = [rootView popoverController];
//HERE WE GO!!! popoverController is a property of iPadRootViewController's instance rootView, hereby referred to as pc.

[pc dismissPopoverAnimated:YES];
//bye bye, popoverController!

希望这可以帮助

推荐阅读
路人甲
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有