关闭 Popover 并一键打开新的 Popover

发布于 2024-12-03 22:37:18 字数 2409 浏览 5 评论 0原文

苹果公司的“移动人机界面指南”对 Popover 的描述如下:

如果可能,允许人们关闭一个弹出窗口并打开一个新弹出窗口 只需轻轻一按。当多个 不同的栏按钮每个都会打开一个弹出窗口,因为它阻止人们 无需进行额外的点击。

我现在能想到的唯一解决方案是在关闭弹出窗口时跟踪触摸的位置,并检查这是否是另一个按钮的位置。 有没有更简单的方法来做到这一点?

PS:我在 stackoverflow 上搜索并在发布之前用 google 搜索了很长一段时间。抱歉,如果之前有人问过这个问题。

更新

我想我没有很好地解释自己。假设我有三个按钮。他们所有人都打开一个弹出窗口。我的用户点击按钮 #1 并打开一个弹出窗口。当弹出窗口打开时,用户点击按钮 #2。弹出窗口被关闭(因为用户点击了弹出窗口的外部 - 非模式弹出窗口的默认行为),并且由于用户单击了按钮 #2,所以打开了一个新的弹出窗口。所有这些都无需点击两次:一次关闭弹出窗口,两次打开新弹出窗口。

第二次更新

我构建了一个简单的虚拟对象来重现我想要做的事情。当用户点击按钮并打开弹出窗口时,不会调用打开弹出窗口的方法。因此,用户必须单击两次才能打开第二个弹出窗口。有什么想法吗?

#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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

但可醉心 2024-12-10 22:37:19

假设您有 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.

辞取 2024-12-10 22:37:18

如果您在工具栏中使用栏按钮项目,则当您点击另一个栏按钮项目时,打开的弹出窗口不会自动关闭。在这些情况下,您应该关闭可见的弹出窗口并一步打开另一个弹出窗口。

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.

囍笑 2024-12-10 22:37:18
- (IBAction)sortAction {
[searchBarView resignFirstResponder];
[self.popoverController dismissPopoverAnimated:YES]; //clear popover

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

希望能帮助你

- (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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文