Question

I choose a picker view from here. Its working great. When app runs it shows up. However, I want this to be shown when textfield is tapped and before that I want this to be hidden. Country picker is the subclass of UIPickerView and is placed inside the nib file. Please help.

Was it helpful?

Solution

You should follow next steps:

    1. Create IBOutlet properties in your owner class (do not forget to add UITextFieldDelegate in your header file):

@property (weak, nonatomic) IBOutlet CountryPicker *myPickerView;
@property (weak, nonatomic) IBOutlet UITextField *myTextField;

    2. Connect your IBOutlets with your PickerView, TextField in xib. and in your viewDidLoad method add:

self.myTextField.delegate = self;

Since now you should implement UITextField delegate methods. UITextField Protocol Reference

    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
      self.myPickerView.hidden = NO;
    }

This method notifies the delegate that the specified text field just became the first responder. You can use this method to update your delegate’s state information. For example, you might use this method to show overlay views that should be visible while editing.

    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
      self.myPickerView.hidden = YES;
    }

This method is called after the text field resigns its first responder status. You can use this method to update your delegate’s state information. For example, you might use this method to hide overlay views that should be visible only while editing.

EDIT1: GitHub Sample

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top