UISplitViewController:如何呈现popover?

时间:2012-01-08 17:37:48

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

我有一个UISplitViewController UISplitViewControllerDelegate,其代理方法如下:

  

splitViewController:willHideViewController:withBarButtonItem:forPopoverController:

当在Portrait中启动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:allowedArrowDirections:animated:]:无法从没有窗口的视图中显示弹出窗口。'

1 个答案:

答案 0 :(得分:4)

只有一个问题,错误的地方调用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];
        }
    }
}