UIPopViewController not working

时光毁灭记忆、已成空白 提交于 2019-12-04 14:19:07

问题


I have an xib file with a .h and .m linked. In the xib there is a UIView with a textView. What I would like to do with that view is open it as a UIPopViewController when you click a button.

Here is my code:

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

It crashed with the following error.

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

I don't understand the error.


回答1:


You can use UIPopoverController only in iPad application.

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



回答2:


UIPopoverController works on iPad only. In iOS 8 you can use UIPopoverPresentationController for both iPhone and iPad, and there's a small trick to make it look like UIPopoverController which is explained HERE.


Here's the Objective-C version of the swift code you see in the link I provided.

@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



回答3:


This must just work for you guys

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


来源:https://stackoverflow.com/questions/28143309/uipopviewcontroller-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!