1

I'm trying to create a simple popover somewhere on the screen, but for some reason it just keeps crashing. It doesn't give me any erro (Zombie Objects are enabled)

UIViewController *viewController = [[UIViewController alloc] init];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
view.backgroundColor = [UIColor redColor];
viewController.view = view;

UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:viewController];
[popover presentPopoverFromRect:CGRectMake(0, 0, 100, 100) inView:self.view.superview permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

I'm just trying to create a popover at a random position, is this not possible?

EDIT: Also tried it like this

@property (nonatomic, retain) UIPopoverController *popover;

@synthesize popover = _popover;

UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.backgroundColor = [UIColor redColor];

_popover = [[UIPopoverController alloc] initWithContentViewController:viewController];
_popover.delegate = self;
[_popover presentPopoverFromRect:CGRectMake(0, 0, 200, 200) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

EDIT2: It might be relevant and I forgot to mention is, but this code is being called in another view.

This works fine!

UIViewController *viewController = [[UIViewController alloc] init];
viewController.contentSizeForViewInPopover = CGSizeMake(200, 200);
viewController.view.backgroundColor = [UIColor redColor];

UIPopoverController *popver1 = [[UIPopoverController alloc] initWithContentViewController:viewController];
[popver1 presentPopoverFromRect:CGRectMake(250, 200, 200, 200) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

DocumentViewController *document = [[DocumentViewController alloc] initWithIssue:_readerModel.currentIssue];
[self.navigationController pushViewController:document animated:YES];

When I call the same EXACT code inside DocumentViewController it doesn't work.

John Riselvato
  • 12,854
  • 5
  • 62
  • 89
woutr_be
  • 9,532
  • 24
  • 79
  • 129
  • Your viewcontroller already have a view - why are you affecting a view to its view property? This doesn't sound right. – tiguero Aug 30 '12 at 09:22
  • @tiguero Sure, but that's not the problem here right now, I just made this as an example to show the code. – woutr_be Aug 30 '12 at 09:24

3 Answers3

3

Hmmm. Can't see no reason for a crash here (code works fine when I test it).

But: If self.view.superview from your code is nil it will crash with a message like

 [...] Popovers cannot be presented from a view which does not have a window [...]

And: There are also some more fixes to do if you want to present the popover properly:

  • Set contentSizeForViewInPopover property in your viewController to manage the real size of your popover.

  • Change UIPopoverArrowDirectionDown to UIPopoverArrowDirectionAny (especially when you use a rect with origin=(0,0) to present from). Otherwise you won't see the popover.

Kai Huppmann
  • 10,705
  • 6
  • 47
  • 78
  • Thanks for the explanation, but unfortunately it doesn't help me any further, i changed `self.view.superview` back to `self.view` since it was `nil`. Added the `contetnSizeForViewInPopover` and change the arrow direction to any, but still it just crashes without any warning when i call `presentPopoverFromRect` – woutr_be Aug 30 '12 at 09:44
  • So there is no message in your Xcode console about the crash? Do you test on a real device? If so: Did you check device logs? – Kai Huppmann Aug 30 '12 at 09:46
  • No message whatsoever, just `Thread 1: signal SIGTRAP`, I don't have a real device at hand atm. – woutr_be Aug 30 '12 at 09:50
  • Googling for SIGTRAP tells me that it could be all about Xcode/Debugger and has nothing to do with your code. 1st thing I'd try: restart Xcode. – Kai Huppmann Aug 30 '12 at 10:07
  • I tried restarting before after I google SIGTRAP, but that doesn't seem to help much, maybe just restarting my entire computer. – woutr_be Aug 30 '12 at 10:30
2

you can check if the window is nil before presenting popover by

if (self.view.window != nil)

also are you calling the above code in viewDidLoad OR viewWillAppear?

Can you try the same code from viewDidAppear method or didMoveToWindow method

woutr_be
  • 9,532
  • 24
  • 79
  • 129
prasad
  • 1,976
  • 2
  • 28
  • 51
  • `self.view.window` is indeed `nil`, `viewDidAppear` doesn't seem to be called. – woutr_be Aug 30 '12 at 10:32
  • may be you are calling this code from a class which added as a subview, so viewDidAppear method will not get called in subview class. Can you call the method which contains popover related code from viewDidAppear of superview class. Also check [this](http://stackoverflow.com/questions/3377001/popovers-cannot-be-presented-from-a-view-which-does-not-have-a-window) similar question. – prasad Aug 30 '12 at 10:52
1

I guess you should define popover as a property, not a local variable

user1203650
  • 300
  • 2
  • 3