0

I am using below code for popover for iPad apps. Its working fine for iPad apps but when i use same code for iPhone its getting crash near

" UIPopoverController* removeDefaultPopover=[[UIPopoverController alloc]initWithContentViewController:NavController];"

Can any help me to come out of this..

 UIViewController  *removeDefaultController = [[UIViewController alloc] init];
        UIView *removeDefaultView = [[UIView alloc] init];
        removeDefaultController.view = removeDefaultView;

        removeDefaultController.contentSizeForViewInPopover = CGSizeMake(100, 100);
        UINavigationController *NavController=[[UINavigationController alloc]initWithRootViewController:removeDefaultController];
        UIPopoverController* removeDefaultPopover=[[UIPopoverController alloc]initWithContentViewController:NavController];
        UIBarButtonItem *edit = [[UIBarButtonItem alloc] initWithTitle:@"EDIT" style:UIBarButtonItemStyleBordered target:self action:@selector(editDefaultLanguage)];
        [removeDefaultController.navigationItem setRightBarButtonItem:edit animated:YES];removeDefaultPopover.delegate=self;
        [removeDefaultPopover presentPopoverFromRect:CGRectMake(0, 0, 100, 100) inView:self.view permittedArrowDirections:NO animated:YES];
        removeDefaultView.backgroundColor=[UIColor redColor];
Nithinbemitk
  • 2,710
  • 4
  • 24
  • 27
  • possible duplicate of [popoverviewController crash on iPhone, works on iPad](http://stackoverflow.com/questions/8391176/popoverviewcontroller-crash-on-iphone-works-on-ipad) – Alexander Jul 30 '14 at 12:23

4 Answers4

2

From the documentation:

Popover controllers are for use exclusively on iPad devices. Attempting to create one on other devices results in an exception.

You have to implement similar functionality by yourself.

Alexander
  • 8,117
  • 1
  • 35
  • 46
2

According to Mark Sands UIPopoverController contains the following code:

- (id)initWithContentViewController:(UIViewController *)viewController {
  if (([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)]) {
      if ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPad) {
          if ([UIPopoverController _popoversDisabled]) {
              [NSException raise:NSInvalidArgumentException format:@"-[UIPopoverController initWithContentViewController:] called when not running under UIUserInterfaceIdiomPad."];
          }
      }
  }    
   ...
}

+ (BOOL)_popoversDisabled {
    NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
    if ([bundleIdentifier isEqualToString:@"com.apple.iBooks"] || [bundleIdentifier isEqualToString:@"com.apple.mobilesafari"] || 
      [bundleIdentifier isEqualToString:@"com.apple.itunesu"] || [bundleIdentifier isEqualToString:@"com.apple.Maps"]) {
      return NO;
  }
  return YES;
}

As you can see UIPopoverController is enabled for Apple's applications.

You can create UIPopoverController subclass and implement + _popoversDisabled in the following way:

+ (BOOL)_popoversDisabled {
  return NO;
}

Or use method swizzling for it.

Sviatoslav Yakymiv
  • 7,887
  • 2
  • 23
  • 43
1

You can't use "UIPopOverController" for iPhone apps as this is intended for iPad devices only. Alternatively you can use CMPopTipView which is work in similar way as UiPopoverController does. You can check this control on https://github.com/chrismiles/CMPopTipView

I have used this control in my previous app and it works great.

Dhawal Dawar
  • 354
  • 2
  • 8
0

As mentioned previously, UIPopoverController is not currently enabled in iPhone applications. However, if you would still like the functionality I would recommend WEPopover. Here's how easy it is to use:

SettingsViewController *viewController = [[SettingsViewController alloc]init];
self.popover = [[WEPopoverController alloc]initWithContentViewController:viewController];

[self.popover presentPopoverFromRect:self.settingsButton.frame
                              inView:self.view
            permittedArrowDirections:UIPopoverArrowDirectionAny
                            animated:YES];

All the correct lifecycle methods are called and it has several convenience methods as well for presenting, dismissing, etc. I've used it in several projects and have been very satisfied with it.

The only thing to watch out for when using this is to make sure that you keep a strong reference to your popover object (thus, in my example you see me use self.popover instead of an instance variable).

ebandersen
  • 2,362
  • 26
  • 25