在运行时创buildpopup视图锚的button

这可能是不可能的,但我希望有人会有一个想法如何做到这一点。

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

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视图来完成,所以我使用Utility App模板中的导航栏和popupbutton来设置初始视图。 但现在我卡住了。 我无法弄清楚如何在运行时创buildpopoverbutton,并使其正确执行popOver视图。 我可以像这样添加button:

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

但是我得到一个exception:'NSInternalInconsistencyException',原因:'UIStoryboardPopoverSegue必须从一个酒吧button项目或视图呈现'。 我很确定这是因为我没有为popOver segue设置锚点。 该故事板中不存在该button,因此我无法将其设置在此处。 我似乎无法find一个API来设置它在运行时。

我也尝试在IB中创buildbutton,但不是在视图层次结构中,然后将rightBarButtonItem属性设置为我现有的button。 这也适用,但我仍然不能将该button设置为popup视图的锚点。 我可以将导航栏设置为定位点,但这使得它定位到导航栏中的标题,看起来很愚蠢。

有任何想法吗?

我有同样的问题,并通过在视图控制器的故事板创build一个UIBarButtonItem而不是视图层次结构的一部分来解决它。

在IB中,将一个栏button项拖到视图控制器视图下面的黑条中,将其放在“First Responder”和“View Controller”图标旁边。 为它创build(强)IBOutlet。 然后,通过从条形button项目拖动到目的地,从其中创build一个popup窗口到目标视图控制器。 看起来这是把它作为主播的唯一方法。 select它作为现有的segue的锚不起作用(看起来像一个IB错误)。

在这里输入图像说明

在viewDidLoad中,您可以将此条button项目分配给navigationItem(或者您喜欢的任何位置),并且segue按预期工作。

我也很好奇,所以我做了一个快速testing项目。 你是对的,似乎没有办法在运行时configurationpopover segue,或者使用Interface Builder将一个锚点添加到不在视图层次结构中的button。

我的解决scheme是在IB中使用可见的UIBarButtonItem设置所有东西,并连接到IBOutlet属性,然后将其从-viewDidLoad的导航栏中-viewDidLoad

 - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.rightBarButtonItem = nil; } 

然后,我只需将其添加回去或​​通过点击另一个button将其删除:

 - (IBAction)toggleBarButtonItem:(id)sender { UIBarButtonItem *item = (self.navigationItem.rightBarButtonItem == nil) ? self.popoverBarButtonItem : nil; [self.navigationItem setRightBarButtonItem:item animated:YES]; } 

你可以有条件地保持或删除按照同样的方式在-viewDidLoad中的button。 segue保持锚定到UIBarButtonItem

我要尝试制作一个虚拟视图,这个视图的大小和形状是我想要呈现的popup窗口的大小和形状,将它连接到seguepopup窗口的目标,然后将视图移动到prepareForSegue:sender:的正确位置prepareForSegue:sender:

我不确定这正是你想要的,但这是我所要做的。 创build一个button,并设置一些目标/行动。 调用该目标/操作方法

 presentPopover:(UIButton *)sender; 

然后在现在的翻页方法,说

 UIViewController *customAdditionalViewController = [[MySpecialVC alloc] init]; //Configure the customAdditionalViewController //Present the customAdditionalViewController in a Popover UIPopoverController *popover = [[UIPopoverController alloc] initWithViewController:customAdditionalViewController]; //Set popover configurations [popover presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:/*whatever you want*/ animated:YES]; 

这就是我将如何处理你的用例。