在运行时为弹出窗口视图锚点创建按钮 [英] creating button for popover view anchor at run time

查看:104
本文介绍了在运行时为弹出窗口视图锚点创建按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是不可能的,但我希望有人知道该怎么做。

This may not be possible, but I'm hoping someone will have an idea how to do it.

我有一个应用程序我只从iPhone移植到环球影城。在iPhone上,我使用的是Tabbed应用程序。我使用三个选项卡显示正常数据。我有一个第四个选项卡,只有在满足某些条件时才会显示。要添加标签,我这样做:

I have an app I'm porting from iPhone only to Universal. On the iPhone, I'm using a Tabbed application. I use three tabs for the normal data to be displayed. I have a forth tab that's only displayed if certain conditions are met. To add the tab, I do:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
{
    UITabBarController *tabController = (UITabBarController *) self.rootViewController;
    NSMutableArray* newArray = [NSMutableArray arrayWithArray: tabController.viewControllers];
    [newArray addObject: [theStoryboard instantiateViewControllerWithIdentifier: @"AdditionalView-Phone"]];
    [tabController setViewControllers:newArray animated:YES];
}

对于iPad,我在初始视图上有足够的空间来显示来自iPhone UI中的三个主要选项卡。所以我需要的是附加数据的另一个(小)视图。我想使用popOver视图来完成它,所以我使用Nav bar和popover按钮设置初始视图,如Utility App模板中所示。但现在我被卡住了。我无法弄清楚如何在运行时创建弹出窗口按钮并使其正确执行popOver视图。我可以像这样添加按钮:

For the iPad, I have enough space on the initial view to display everything from the main three tabs in the iPhone UI. So all I need is one additional (small) view for the "Additional" data. I wanted to do it using a popOver view, so I set up the initial view with a Nav bar and popover button as in the Utility App template. But now I'm stuck. I can't figure out how to create that popover button at run time and make it do the segue to the popOver view properly. I can add the button like this:

UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] initWithTitle: @"Modem" style: UIBarButtonItemStylePlain target: self action: @selector(togglePopover:)];
self.navBar.topItem.rightBarButtonItem = flipButton;

但是我得到一个例外:'NSInternalInconsistencyException',原因:'UIStoryboardPopoverSegue必须从一个条形按钮显示项目或视图。'我很确定这是因为我没有为popOver segue设置锚点。故事板中不存在该按钮,因此我无法将其设置在那里。我似乎无法在运行时找到一个API来设置它。

but I get an exception: 'NSInternalInconsistencyException', reason: 'UIStoryboardPopoverSegue must be presented from a bar button item or a view.' I'm pretty sure this is because I don't have an anchor set for the popOver segue. The button doesn't exist in the storyboard, so I can't set it there. And I can't seem to find an API to set it at run time.

我也尝试在IB中创建按钮,但不在视图层次结构中创建,然后只需将rightBarButtonItem属性设置为现有按钮即可。这也有效,但我仍然无法将该按钮设置为弹出视图的锚点。我可以将导航栏设置为锚点,但这会使它锚定到导航栏中的标题,这看起来很傻。

I also tried creating the button in IB, but not in the view hierarchy, and then just setting the rightBarButtonItem property to my existing button. That also works, but I still can't set that button as the anchor for the popover view. I can set the Navigation Bar as the anchor, but that makes it anchor to the title in the nav bar, which looks silly.

任何想法?

推荐答案

我遇到了同样的问题,并通过在视图控制器的故事板中创建UIBarButtonItem但不是视图层次结构的一部分来解决它。

I had the same problem and solved it by creating a UIBarButtonItem in the Storyboard for the view controller but not part of the view hierarchy.

在IB中,将条形按钮项拖动到视图控制器视图下方的暗条,将其放在第一响应者和视图控制器图标旁边。为它创建一个(强大的)IBOutlet。然后通过从条形按钮项拖动到目标,从它创建一个popover segue到目标视图控制器。看起来这是将它设置为锚点的唯一方法。选择它作为现有segue的锚点不起作用(看起来像IB错误)。

In IB, Drag a bar button item to the dark bar below the view controller view, drop it next to the "First Responder" and "View Controller" icons. Create a (strong) IBOutlet for it. Then create a popover segue from it to the destination view controller by dragging from the bar button item to the destination. It seems like this is the only way to set it as the anchor. Choosing it as the anchor for an existing segue does not work (looks like an IB bug).

在viewDidLoad中,您可以将此栏按钮项指定给navigationItem(或您喜欢的地方)和segue按预期工作。

In viewDidLoad you can assign this bar button item to the navigationItem (or where ever you like) and the segue works as expected.

这篇关于在运行时为弹出窗口视图锚点创建按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆