TheInterestedOne
TheInterestedOne

Reputation: 768

How to hide a UIPickerView when the user make its choice

I've created a custom UIPickerView with the following code

UIPickerView *picker =[[UIPickerView alloc] initWithFrame:CGRectMake(139,50,161,30)];
    picker.delegate=self;
    picker.showsSelectionIndicator=YES;
    picker.hidden=NO;

    [self.view addSubview:picker];

Now I want to hide the pickerView when the user make its choice of a row simply with

picker.hidden=YES;

Now: 1) How can I recognize the choice of the user and then hide the unuseful pickerview? 2) Can I show the choice in a TextField? with @"choice"?

Upvotes: 6

Views: 18832

Answers (7)

Marcin Żmigrodzki
Marcin Żmigrodzki

Reputation: 19

In XCode 12.2 you could use:

thePickerView.isHidden = true

Upvotes: 0

5573352
5573352

Reputation: 1135

I'm using a UIPickerView as the inputView for a TextField (for replacing the standard keyboard). I use the ResignFirstResponder (TextField method) to dismiss the view.

Upvotes: 0

Ankit Goyal
Ankit Goyal

Reputation: 3049

UIView *addMealView; // here i am showing my picker

   - (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component 
{   
[addMealView endEditing:YES];   
}

Upvotes: 0

Pétur Ingi Egilsson
Pétur Ingi Egilsson

Reputation: 4442

I'm using a UIPickerView as the inputView for a TextField (thus replacing the onscreen keyboard). I use the following delegate to dismiss the view.

#pragma mark - UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    // Code logic
    [[self view] endEditing:YES];
}

Upvotes: 5

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39978

Add this line where you have created picker

picker.delegate = self;

your callback

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    picker.hidden=YES;
}

Upvotes: 2

Paras Joshi
Paras Joshi

Reputation: 20541

use the delegate method of UIPickerView like bellow..

   - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

        yourTextField.text = [yourArray objectAtIndex:row];
        thePickerView.hidden = YES;

    }

Also you can take one UIButton and hide this with action event of UIButton.

Upvotes: 2

Shashank Kulshrestha
Shashank Kulshrestha

Reputation: 1556

You can use UINavigationBar and Barbutton with cancel and Done text upon it.

You can refer the following link

Objective C implementing a UIPickerView with a "Done" button

Upvotes: 0

Related Questions