在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;
}
这可以很好地显示按下按钮时的弹出窗口。但是,如果按下按钮,我也希望让按钮解除,因为它已经打开以遵循良好的指导方针。我该怎么做呢? (即如果用户反复点击此按钮,则弹出窗口应该隐藏其他所有击中。)
答案 0 :(得分:16)
当splitViewController显示popover时,将调用下面的方法。 只检查是否为零,然后解雇它:)
- (void)splitViewController:(UISplitViewController*)svc popoverController:(UIPopoverController*)pc willPresentViewController:(UIViewController *)aViewController{
if ([pc isPopoverVisible]) {
[pc dismissPopoverAnimated:YES];
}
}
答案 1 :(得分:7)
Apple的HIG说在弹出框中不应该有一个明确的解雇按钮,但要做你要求的,你有两个选择。
1)发布NSNotification
OR
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];
执行此操作会向应用程序发送通知,并且由于您已注册其他视图控制器以进行侦听,因此每当它看到该通知时,它都会调用您指定的选择器,在这种情况下,将解除此选项。
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!
希望这有帮助
答案 2 :(得分:3)
这很容易,因为popoverController是一个属性。使参考更容易。
if ([self.popoverController isPopoverVisible]) {
//using the setters and getters "goes thru the proper channels" when accessing objects
[self.popoverController dismissPopoverAnimated:YES];
} else {
UIPopoverController *pc = [[UIPopoverController alloc] initWithContentViewController:YOUR_VIEW_CONTROLLER];
self.popoverController = pc;
[pc release];
//get the button instance you set on the toolbar
UIBarButtonItem *categoryButton = [[toolbar items] objectAtIndex:0];
[self.popoverController presentPopoverFromBarButtonItem:categoryButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
我实际上刚刚意识到你是指代Delegate方法中的代码,用于在你的splitView的index:0处显示viewController。这个答案不一定适用于此,但适用于您在iPad上访问和创建popoverControllers的任何其他时间。在不检查弹出窗口是否可见的情况下,您将要么崩溃,要么打开几个弹出窗口。
感谢您的时间。
答案 3 :(得分:1)
您可以尝试下面的
if(![popoverController isPopoverVisible]){
// Show popover
}
else{
// close popover
[popoverController dismissPopoverAnimated:YES];
}
答案 4 :(得分:0)
我用来在RootViewController.m中显示popover的代码:
- (IBAction) addCategory:(id)sender {
AddCategoryViewController *content = [[AddCategoryViewController alloc] init];
UIPopoverController *aPopover = [[UIPopoverController alloc]
initWithContentViewController:content];
aPopover.delegate = self;
// Store the popover in a custom property for later use.
self.addCategoryPopover = aPopover;
addCategoryPopover.delegate = self;
[aPopover release];
[content release];
[addCategoryPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
我过去常常尝试从另一个类中解雇它:
-(IBAction)saveAddCategory:(id)sender {
rootViewController = [[RootViewController alloc] init];
[rootViewController dismissPopover];
}
我的dismissPopover
功能如下:
- (void) dismissPopover {
if ([self.addCategoryPopover isPopoverVisible]) {
[self.addCategoryPopover dismissPopoverAnimated:YES];
}
if (addCategoryPopover.popoverVisible == YES) {
[addCategoryPopover dismissPopoverAnimated:YES];
}
}
答案 5 :(得分:0)
如果您使用默认的UISplitViewController
设置,则创建的导航栏按钮会显示RootViewController
的缩位。
如果您想确保一次没有多个弹出窗口,只需在出现RootViewController
时即可关闭弹出式窗口。这是我用来解决这个问题的代码:
- (void) viewWillAppear:(BOOL)animated {
if ([self.popover isPopOverVisible]) {
[self.popover dismissPopoverAnimated:YES];
}
[super viewWillAppear:YES];
}
答案 6 :(得分:0)
是的,您可以将modalPresentationStyle
设置如下:
controller.modalPresentationStyle = UIModalPresentationFormSheet;