alexkent
alexkent

Reputation: 1596

lay out a view in Storyboard then load it in a PopOver from code?

I have relatively complex ui appearing in a popover (complex enough that doing all the layout and relationships from code would be a pain), but the button that calls it is created and placed into the parent view (which exists in the storyboard) from code.

I've tried making a popover segue from the parent view's viewcontroller object to the popover content vc, then triggering this with performSegueWithIdentifier. This almost works, but I can't figure out how to set the popOver's Anchor from code so it appears at the wrong place (squished at the bottom of the screen).

Is there a way to set the popOver segue's Anchor dynamically? or Can i create a UIPopOverController object and get the view i've put together in the storyboard into it? or Is there some other obvious way to do this that I'm not seeing?

please be gentle, I'm new here.

iPad iOS5.1 XCode4.3.2

Upvotes: 3

Views: 2546

Answers (1)

Matt Long
Matt Long

Reputation: 24486

Alex,

I'm not completely sure I understand what you're trying to do, but let me take a stab at it from what I think I understand.

For the same reason you cite (view complexity, etc.), I often build out my views in the storyboard and then load them from some action. What you can do is instantiate the view controller by identifier with something like this:

FancyViewController *controller = [[self storyboard] 
              instantiateViewControllerWithIdentifier:@"FancyViewController"];

This assumes you have created a UIViewController subclass called FancyViewController and have set the type for your view controller in the storyboard.

Now, you can display the view controller in a popover controller or you can push it onto a navigation stack. You just need to make sure you've set the identifier for your view controller in the storyboard.

enter image description here

Also, you'll probably want to instantiate your view controller once if you use a popover controller and just update the view controllers properties each time the action gets triggered. So, if it's tapping a button that triggers the popover, your code might look like this:

- (IBAction)didTapButtonToShowFancyViewController:(id)sender
{
  if (![self fancyViewController])
  {
    // fancyViewContrller is a property of type FancyViewController *
    fancyViewController = [[[self storyboard]
          instantiateViewControllerWithIdentifier:@"FancyViewController"];

    // fancyViewPopoverController is also a property
    fancyViewPopoverController = [[UIPopoverController alloc] 
                  initWithContentViewController:fancyViewController];
  }

  // Perform setup on the fancy controller you want to do every
  // time the action gets triggered here. Do initialization in the if
  // block above.

  // Now display the popover from the sender's frame. I'm assuming the
  // sender is a UIButton.
  [fancyViewPopoverController presentPopoverFromRect:[sender valueForKey:@"frame"] 
                              inView:[self view] 
              permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

The only way to set the popover's "anchor" dynamically is to use an explicit action that calls presentPopoverFromRect:permittedArrowDirections:animated: instead of a segue.

I hope that helps. Let me know if I've misunderstood what you're trying to do.

Best regards.

Upvotes: 8

Related Questions