2

I am trying to present a UIViewController inside a UIPopoverController. I've designed a view controller in Interface Builder, gave it a custom class (let's say MyAppTestViewController) and I'm trying to create a popover this way:

MyAppTestViewController *fxViewController = [MyAppTestViewController new];

self.fxPopover = [[UIPopoverController alloc] initWithContentViewController:fxViewController];
self.fxPopover.popoverContentSize = CGSizeMake(1024, 120);

[self.fxPopover presentPopoverFromBarButtonItem:_fxButton permittedArrowDirections:UIPopoverArrowDirectionDown animated:NO];

When I press the button, a popover is displayed at the correct place with the correct size, but it is empty. I've verified that the MyAppTestViewController's viewDidAppear method is being called (by NSLog), but the Popover's inside is empty:

ipad screenshot

Am I missing something?

Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389

2 Answers2

2

I see that you mentioned in a comment that you're using a storyboard. So why not use a popover segue to your MyAppTestViewController? You could wire the segue directly to the Effects button on your toolbar. (Or, alternatively, call performSegueWithIdentifier: from your presenting view controller.) You might do a quick test by just throwing a UILabel into MyAppTestViewController right on the storyboard and seeing if it displays properly.

enter image description here

Jay Slupesky
  • 1,893
  • 13
  • 14
1

I think the problem is here:

MyAppTestViewController *fxViewController = [MyAppTestViewController new];

Generally you would use [[MyViewControllerClass alloc] initWithNibName:nil bundle:nil] (assuming you have a xib file with a matching name). I don't believe I have ever seen a view controller initialized with new. Everything in Objective-C is alloc-init.

Apple Docs: UIViewController Class Reference

Quote:

To initialize your view controller object using a nib, you use the initWithNibName:bundle: method to specify the nib file used by the view controller. Then, when the view controller needs to load its views, it automatically creates and configures the views using the information stored in the nib file.


EDIT:

Fascinating, well okay. It looks like you are right about the use of the new keyword, here is bit of an explanation of this.

So fine, that's not the problem. Have you tried breaking on the viewDidAppear method and using the debugger to print out the view properties, check its frame, check its superview, and so on, try to understand the problem better? You may already know how to do this, but the commands would be po self.view and so on.

In any case, I also found this, although it only goes into the mechanics of popover presentation and not content view assignment, which you seem to have down.

Community
  • 1
  • 1
Matt Mc
  • 8,882
  • 6
  • 53
  • 89
  • no, i've been initializing lots of view controllers with `new` with success, it's just a shorthand for alloc-init. i am not using a seperate nib, it's a view controller from my storyboard. – Can Poyrazoğlu Oct 28 '12 at 12:40
  • the link that you've provided in the end is really useful, thank you. i've finally decided to implement the popover control visually by presenting a popover segue, as answered by Jay Slupesky. i find visual design cleaner, and try to avoid code while designing as much as possible.. thanks for the link anyway. – Can Poyrazoğlu Oct 28 '12 at 21:17