Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Storyboard to make popover that can be used in code?

I'm building a collection of forms each of which contains several fields. Some of the fields are UITextFields that will display a date. I've created a new class called DatePickerTextField, a descendant of UITextField. When a DatePickerTextField is tapped I'd like for a UIDatePicker control to appear in a popover.

My question is how do I use the storyboard to implement the popover? I can do a segue when there is a specific, visible control in the scene. But how do I represent a generic popover in the scene that I can attach to any instantiated DatePickerTextField that becomes active?

like image 627
David U Avatar asked Jun 22 '12 18:06

David U


2 Answers

You can create segue that is not connected to any control but I don't think that there would be way to specify anchor point for popover from code. Another option is to create ViewController that is not connected with any segue. When editing storyboard, create ViewController which will be placed in popover, select it and navigate to Utilities pane->Attributes Inspector. Set Size to Freeform, Status Bar to None, specify unique Identifier that will be used to instantiate ViewController from code. Now you can change the size of ViewController by selecting its View and navigating to Utilities pane->Size Inspector.

After that you can create popover from code:

- (IBAction)buttonPressed:(id)sender {
    UIView *anchor = sender;
    UIViewController *viewControllerForPopover = 
        [self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"];

    popover = [[UIPopoverController alloc] 
               initWithContentViewController:viewControllerForPopover];
    [popover presentPopoverFromRect:anchor.frame 
                             inView:anchor.superview 
           permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

One caveat is that you need to hold reference to popover as ivar of your class, otherwise it'll crash because UIPopoverController would be released and deallocated after buttonPressed returns:

@interface MyViewController : UIViewController {
//  ...
    UIPopoverController *popover;
//  ...
}
like image 136
Johnnywho Avatar answered Nov 09 '22 23:11

Johnnywho


So, I had a similar issue, and in case others might benefit, I figured I'd share it, since I benefit so much from stackoverflow.

This solution allows you to set the anchor of a customizable popover segue. It also allows you to configure the segue to be modal or not (I could not find a way to prevent the segue by dimming the exterior context, so if someone knows how to do that, I would be interested in hearing it); this is accomplished by setting the passthrough views for the popover controller. I also added the capacity to specify a custom view, rather than the view of the source viewcontroller (since I needed this capacity); this portion is not critical to the solution.

DynamicPopoverSegue.h

@interface DynamicPopoverSegue : UIStoryboardPopoverSegue

@property BOOL isModal;
@property UIView* sourceView;
@property CGRect anchor;

@end

DynamicPopoverSegue.m

@implementation DynamicPopoverSegue

- (void)perform
{
   if (!self.popoverController.popoverVisible)
   {
      UIViewController* dst = (UIViewController*)self.destinationViewController;
      UIViewController* src = (UIViewController*)self.sourceViewController;

      UIView* inView =  _sourceView ? _sourceView : src.view;

      self.popoverController.contentViewController = dst;
      if (!_isModal)
      {
         [self.popoverController setPassthroughViews:[[NSArray alloc] initWithObjects:inView, nil]];
      }
      [self.popoverController presentPopoverFromRect:_anchor
                                              inView:inView
                            permittedArrowDirections:UIPopoverArrowDirectionAny
                                            animated:YES];
   }
}

@end

Then you just set your segue to "Custom" in the storyboard, and set the segue class to "DynamicPopoverSegue". In my case, since I wanted to associate it with dynamic layers in a view, I could not set the anchor, so I created the segue by control clicking from the view controller icon in the bar beneath my view controller to the view controller I was using to present the popupover.

To call the popover segue:

[self performSegueWithIdentifier:@"MyPopoverSegue" sender:self];

And to configure the popover segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   if ([[segue identifier] isEqualToString:@"MyPopoverSegue"])
   {
      DynamicPopoverSegue* popoverSegue = (DynamicPopoverSegue*)segue;
      // set the anchor to wherever you want it to be
      popoverSegue.anchor = _destinationFrame;
   }
}
like image 2
Thomas Lechner Avatar answered Nov 09 '22 22:11

Thomas Lechner