I'm building a collection of forms each of which contains several fields. Some of the fields are UITextField
s 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?
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;
// ...
}