【问题标题】:Storyboard - Open UIViewController as UIPopoverControllerStoryboard - 打开 UIViewController 作为 UIPopoverController
【发布时间】:2014-05-24 19:51:01
【问题描述】:

对于我的 iPad 版本的应用程序,我想通过单击按钮以 UIPopoverController 的形式打开我的 UIViewController。因此,视图控制器打开的真实代码如下。如何轻松地将此类代码转换为将UIViewController 作为UIPopoverController 打开?

代码:

UIStoryboard *sb = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary        objectForKey:@"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]];

UIViewController *aboutView = [sb instantiateViewControllerWithIdentifier:@"AboutViewController"];
aboutView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:aboutView animated:YES completion:NULL];

【问题讨论】:

  • 只使用情节提要中的弹出框转场

标签: ios iphone objective-c xcode uiviewcontroller


【解决方案1】:

使用类似的东西:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    UIPopoverController *poc = [[UIPopoverController alloc] initWithContentViewController:aboutView];
    [poc presentPopoverFromRect:CGRectMake(somePoint.x, somePointY, 1.0, 1.0) inView:self.view permittedArrowDirections: UIPopoverArrowDirectionAny animated:YES];
} else {
    aboutView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:aboutView animated:YES completion:NULL];
}

您还可以通过按钮显示弹出框,还可以控制弹出框的显示方向。如果您使用UIPopoverArrowDirectionAny,iOS 将做出明智的决定,让弹出框可见。

您还应该保持对弹出框的强烈引用,并且只在它是nil 时显示它,以确保弹出框只出现一次。这意味着如果弹出框被关闭,请将持有它的属性设置为nil

【讨论】:

  • 另外,很高兴知道如何关闭它。我会认为 [self dismissPopoverAnimated:YES];可以解决问题,但编译器看不到它,因为它是一个视图控制器。
  • Apple 关于dismissPopoverAnimated:You can use this method to dismiss the popover programmatically in response to taps inside the popover window. Taps outside of the popover’s contents automatically dismiss the popover. 所以如果你需要在代码中关闭它,你需要在popover viewcontroller 本身上调用它,而不是在呈现的viewcontroller 上调用它。此外,如果 poc 自动关闭,请使用其委托方法 - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController 将指向它的指针设置为 nil,如答案中所述。
【解决方案2】:

一个常见的解决方案是有两个故事板,一个用于 iPhone,一个用于 iPad。这样您就可以在 iPad 故事板中使用 popover segue 和在 iPhone 故事板中使用模态 segue。您可以轻松安排 Info.plist,以便在启动时自动加载正确的故事板。不过,您仍然需要一些条件代码,因为您的代码对呈现的视图控制器的响应与对弹出框的响应不同。

【讨论】:

    猜你喜欢
    • 2012-01-11
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多