Search code examples
iphoneuitextfieldpresentmodalviewcontrolleruistoryboardsegue

iPhone Presenting modal view from UITextField using segues?


As title states it, i am using iOS5 feature storyboards. I have a text field, which when tapped should display modal view (with picker). I was wondering can this action be triggered using segues?

When you have button or cell you just ctrl drag to view controller that you want to present. I have done same with text field but with no visible results. I just press it and and only keyboard pops. No modal view showing. What should I do?


Solution

  • There are some thoughts to your questions, I hope that they can be helpful:

    • If your viewController adopts UITextFieldDelegate, then you can implement the optional delegate method textFieldDidBeginEditing:, and get notified when the textField becomes first responder and do what you want to do, e.g. presenting a modal view controller (UITextFieldDelegate Protocol Reference). However, when the textField becomes first responder and keyboard pops out, at the same time you will be popping out a modal view too, that doesn't sound very user-friendly.

    • If user has only limited choices to pick from, why using a textField?

    • The other alternative could be implementing textFieldShouldBeginEditing: (again, you need to adopt UITextFieldDelegate). Per Apple,

    When the user performs an action that would normally initiate an editing session, the text field calls this method first to see if editing should actually proceed. In most circumstances, you would simply return YES from this method to allow editing to proceed.

    You will return NO so that the keyboard won't pop, and instead pop your modal view controller. You do this by first calling presentViewController:animated:completion: and then returning NO (UIViewController Class Reference) -- I haven't tested this, but it sounds it should work.

    • I am not 100% sure. I think I heard it somewhere that segues in storyboard are invoked with UIControlEvent equivalent of UIControlEventTouchUpInside -- think of UIButton (I might be wrong), but if this is true, when UITextField becomes first responder, that is not the control event, therefore the segue won't work.