2

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.

3 Answers3

3

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
Community
  • 1
  • 1
Armin
  • 1,150
  • 8
  • 24
  • The link you gave me is described in swift –  Jan 26 '15 at 01:32
  • It's the same thing in Objective-C... Ok i'll translate it for you and update my answer. – Armin Jan 26 '15 at 01:40
  • Thanks! Please let me know when you finish your update –  Jan 26 '15 at 01:44
  • I've included the obj-c equivalent. – Armin Jan 26 '15 at 01:48
  • It worked like a normal new viewController. Meaning it didn't pop over, it went to the next segue –  Jan 26 '15 at 02:02
  • It acted like a full sized viewController. –  Jan 26 '15 at 02:40
  • Try `UIModalPresentationPopover`. Although: "In a horizontally compact environment, this option behaves the same as `UIModalPresentationFullScreen`." – Armin Jan 26 '15 at 02:46
  • I copy-pasted your code but it didn't work because you use a capital S in `Sender` in prepareForSegue, so that method never got called. – ernesto Mar 05 '15 at 10:39
  • What would be the code equivalent? (Without Storyboard and Segues) –  May 12 '15 at 13:24
  • @Armin hey for me also its working as normal .. i want popover with background clear color to view previous viewcontroller – the1pawan Jun 16 '15 at 13:38
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
}
Oleg Sobolev
  • 3,286
  • 2
  • 16
  • 29
0

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;
}
USERX2DX
  • 207
  • 2
  • 8