Hiding/ Showing UIPickerView

后端 未结 8 592
广开言路
广开言路 2020-12-05 11:09

I Have an a touchesEnded event that checks for when a UITextField is pressed. What I would like it to do is is hide/show a UIPickerView. How can this be done?



        
8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 11:27

    Because I saw a comment about these solutions not working on iOS 7 I will assume this thread is still relevant and being searched for.

    The best way I have found to do this is by attaching the UIPickerView to a (hidden)UITextField as the input view like:

    _myPicker = [[UIPickerView alloc] init];
    _myPicker.delegate = self;
    _myPicker.showsSelectionIndicator = YES;
    myTextField.inputView = _myPicker;
    

    You can always hide the text field if desired. Then you can show/hide the UIPickerView by activating the textfield as first responder like:

    [myTextField becomeFirstResponder];
    [myTextField resignFirstResponder];
    

    I have verified this works on iOS 7 and I have had it working as far back as iOS 5.

提交回复
热议问题