Close Popover and open new one with one tap

一曲冷凌霜 提交于 2019-12-06 04:21:40

问题


Apple's "Mobile Human Interface Guidelines" says about Popovers:

When possible, allow people to close one popover and open a new one with one tap. This behavior is especially desirable when several different bar buttons each open a popover, because it prevents people from having to make extra taps.

The only solution I can think right now is to track the position of the touch when dismissing the popover and check whether that was the position of another button. Is there any easier way to do this?

PS: I searched in stackoverflow and googled quite a while before posting. Sorry, if this was asked here before.

UPDATE

I guess I didn't explain myself well. Let's say I have three buttons. All of them open a popover. My user taps button #1 and a popover opens. While the popover is open, the user taps button #2. The popover gets dismissed (because the user tapped outside of the popover - default behavior of non-modal popovers) and a new popover opens up because the user had clicked on button #2. All of that without having to tap twice: once to dismiss the popover and twice for opening the new one.

2nd UPDATE

I built a simple dummy to reproduce what I'm trying to do. When the user taps on a button and a popover is open, the method that opens the popovers doesn't get called. Therefore the user has to click twice to open the second popover. Any ideas?

#import "RootViewController.h"
#import "AViewController.h"

@interface RootViewController() 

@property (nonatomic, retain) UIPopoverController *currentPopover;

@end

@implementation RootViewController

@synthesize currentPopover;

- (void)loadView
{
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    UIView *view = [[UIView alloc] initWithFrame:applicationFrame];

    CGRect buttonFrame = CGRectMake(50, 100, 200, 40);

    for (int i = 0; i < 3; i++) 
    {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button setTitle:[NSString stringWithFormat:@"Button %i", i + 1] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(openPopover:) forControlEvents:UIControlEventTouchDown];
        [button setFrame:buttonFrame];
        [view addSubview:button];

        buttonFrame.origin.y += 50;
    }


    self.view = view;
    [view release];
}

- (IBAction)openPopover:(id)sender
{
    AViewController *avc = [[AViewController alloc] init];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:avc];
    [avc release];

    UIPopoverController *tempPopover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
    [tempPopover setDelegate:self];
    [tempPopover setPopoverContentSize:CGSizeMake(320, 500)];
    [tempPopover presentPopoverFromRect:[sender frame] inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];

    self.currentPopover = tempPopover;
    [tempPopover release];


    [navigationController release];
}

- (void)dealloc
{
    [currentPopover release];
    [super dealloc];
}

@end

回答1:


If you use bar button items in a toolbar, the open popover is not automatically closed when you tap another bar button item. In these situations you should close the visible popover and open the other one in one step.




回答2:


- (IBAction)sortAction {
[searchBarView resignFirstResponder];
[self.popoverController dismissPopoverAnimated:YES]; //clear popover

self.popoverController = popoverSetting;
[self.popoverController presentPopoverFromBarButtonItem:sortBarButtonItem
                               permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //show popover
}

hope will help you




回答3:


Lets say you have 3 buttons and each opens a popup. You could use a state variable that tracks whether a popup is currently open, and inside each "open popup" method, close the existing one (if it is open) before opening the new popup.



来源:https://stackoverflow.com/questions/7390523/close-popover-and-open-new-one-with-one-tap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!