【问题标题】:UIPopViewController not working弹出视图控制器不起作用
【发布时间】:2015-01-26 00:38:51
【问题描述】:

我有一个 .h 和 .m 链接的 xib 文件。在 xib 中有一个带有 textView 的 UIView。当您单击按钮时,我想对该视图执行的操作是作为 UIPopViewController 将其打开。

这是我的代码:

- (IBAction)thisButton:(id)sender
{
    popViewController *popVC = [[popViewController alloc] initWithNibName:@"popViewController" bundle:nil];

    self.pop = [[UIPopoverController alloc] initWithContentViewController:popVC];

    [self.pop setPopoverContentSize:CGSizeMake(220, 120) animated:YES];
    [self.pop presentPopoverFromRect:[(UIButton *)sender frame] inView:self.view
                   permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}

它因以下错误而崩溃。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController initWithContentViewController:] called when not running under UIUserInterfaceIdiomPad.'

我不明白这个错误。

【问题讨论】:

    标签: ios objective-c xib uipopovercontroller


    【解决方案1】:

    UIPopoverController 仅适用于 iPad。在 iOS 8 中,您可以在 iPhone 和 iPad 上使用 UIPopoverPresentationController,并且有一个小技巧可以使它看起来像 UIPopoverController,这在 HERE 中进行了解释。


    这是您在我提供的链接中看到的 swift 代码的 Objective-C 版本。

    @interface SomeViewController : UIViewController <UIPopoverPresentationControllerDelegate>
    @end
    
    @implementation SomeViewController
    
    -(void) prepareForSegue:(UIStoryboardSegue *) segue sender:(id) sender 
    {
        if ([segue.identifier isEqualToString:@"PopoverSegue"]) {
            UIViewController *controller = segue.destinationViewController;
            controller.popoverPresentationController.delegate = self;
            controller.preferredContentSize = CGSizeMake(320, 186);                
        }
    }
    
    // MARK: UIPopoverPresentationControllerDelegate
    
    -(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller 
    {
        // Return no adaptive presentation style, use default presentation behaviour
        return UIModalPresentationNone;
    }
    @end
    

    【讨论】:

    • 你给我的链接是用swift描述的
    • 在 Objective-C 中也是一样的...好的,我会为您翻译并更新我的答案。
    • 谢谢!完成更新后请告诉我
    • 我已经包含了 obj-c 等效项。
    • 它就像一个普通的新视图控制器。这意味着它没有弹出,它进入了下一个segue
    【解决方案2】:

    您只能在 iPad 应用程序中使用 UIPopoverController。

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        // for iPads
        // here you can use UIPopoverController   
    } else
    {
        // for iPhones
    }
    

    【讨论】:

    • 那么在iphone上就没有办法了吗?
    • @MikeRally 不,您应该使用其他方式在 iPhone 上显示当前视图控制器。
    【解决方案3】:

    这一定对你们有用

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"popoverSegue"])
    
        {
            UIViewController *popUpControl=segue.destinationViewController;
            popUpControl.modalPresentationStyle=UIModalPresentationPopover;
            popUpControl.popoverPresentationController.delegate=self;
        }
    }
    -(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
    {
        return UIModalPresentationNone;
    }
    

    【讨论】:

    • 你能不能再补充一些解释?
    猜你喜欢
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 2013-03-08
    相关资源
    最近更新 更多