UIPopViewController 不工作

UIPopViewController not working

我有一个带有 .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.'

我没看懂错误。

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

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

UIPopoverController 仅适用于 iPad。在 iOS 8 中你可以使用 UIPopoverPresentationController for both iPhone and iPad, and there's a small trick to make it look like UIPopoverController which is explained HERE.


这是您在我提供的 link 中看到的 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

这一定对你们有用

-(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;
}