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



当 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];
}

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

最佳答案

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

关于objective-c - UISplitViewController:如何呈现弹出框?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8779674/

10-14 21:59