【问题标题】:UISplitViewController: How to present popover?UISplitViewController:如何呈现弹出框?
【发布时间】:2012-02-05 11:19:05
【问题描述】:

我有一个UISplitViewController,它是一个UISplitViewControllerDelegate,具有以下委托方法:

splitViewController:willHideViewController:withBarButtonItem:forPopoverController:

当 iPad 以纵向启动时,我希望 SplitView 中的 Popover 可见。我该怎么做?

我试过以下代码:

- (void)splitViewController:(UISplitViewController *)svc
     willHideViewController:(UIViewController *)aViewController
          withBarButtonItem:(UIBarButtonItem *)barButtonItem
       forPopoverController:(UIPopoverController *)pc
{
    //setting the barButtonItem in the toolbar in the detail view.

    [pc presentPopoverFromBarButtonItem:barButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}

但是上面的代码给了我以下错误:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]:无法从没有窗口的视图中呈现弹出框。”

【问题讨论】:

    标签: objective-c ios cocoa-touch ipad uisplitviewcontroller


    【解决方案1】:

    只有一个问题,调用 presentPopover 方法的地方不对,splitViewController:*WillHide*ViewController....... 所以,barButtonItem 存在但屏幕上不存在。我使用了下一个代码,它对我有用。 要处理所有情况,您需要使用 2 种方法。

    - (void)viewDidAppear:(BOOL)animated
    {
        if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) {
            if (self.view.window != nil) {
                [_masterPopoverController presentPopoverFromRect:CGRectMake(0, 0, 1, 1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];
            }
        }
        [super viewDidAppear:animated];
    }
    

    -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
        if (fromInterfaceOrientation == UIDeviceOrientationLandscapeLeft || fromInterfaceOrientation == UIDeviceOrientationLandscapeRight) {
            if (self.view.window != nil) {
                [_masterPopoverController presentPopoverFromRect:CGRectMake(0, 0, 1, 1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];
            }
        }
    }
    

    【讨论】:

    • 您将此代码放在代码的什么位置?我试图处理指向我的 ViewController 的 PopoverController 指针,但是当我想呈现弹出框时,它似乎被更改为 nil。
    猜你喜欢
    • 2013-06-06
    • 1970-01-01
    • 2019-02-26
    • 2015-02-05
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多