Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding/ Showing UIPickerView

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?

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    if (CGRectContainsPoint([self.textField frame], [touch locationInView:self.view]))
    {
        NSString * error = @"Touched the TextField";
        UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Selection!" message:error delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [errorAlert show];
        //Want to show or hide UIPickerView
    }
}

I already have an allocated UIPickerView when touches occur

@interface ThirdViewController : UIViewController <UITextFieldDelegate,UIPickerViewDelegate> {
    IBOutlet UIPickerView *pickerView;
}
like image 764
aahrens Avatar asked Apr 14 '10 01:04

aahrens


4 Answers

Toggling the "hidden" property will do the trick, but will also give a very abrupt reveal.

One way to avoid this is to get the picker to slide up from the bottom of the screen by embedding it inside a UIActionSheet.

Here's an example:

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil 
delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

    CGRect pickerFrame = CGRectMake(0, 0, 0, 0);
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;
    pickerView.dataSource = self;
    pickerView.delegate = self;
    [sheet addSubview:pickerView];
    [pickerView release];

    [sheet showInView:view];
    [sheet setBounds:CGRectMake(0, 0, 320, 415)];
    self.actionSheet = sheet; // assuming you have setup a property to hold the action sheet
    [sheet release];

When you've finished with the picker, dismiss it:

[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];

This approach can also be used to incorporate buttons in a bar above the picker ("Done", "Previous", "Next" etc) - there's a good explanation of how to do it here.

like image 99
Jonathan Moffatt Avatar answered Nov 17 '22 01:11

Jonathan Moffatt


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.

like image 24
Joe Avatar answered Nov 17 '22 01:11

Joe


UIPickerView inherits from UIView, so you should be able to just toggle its 'hidden' property:

if (pickerView) pickerView.hidden = !pickerView.hidden;
like image 14
Joe Strout Avatar answered Nov 17 '22 00:11

Joe Strout


I am using a dummy UITextField for showing / hiding the UIPickerView

First, add a UITextField as a @property on your UIViewController. Optionally, add the UIPickerView, as well

@property (strong, nonatomic) UITextField *dummyTextField;

Second, assign a UIPickerView as inputView for the UITextField. Assign yourself as the dataSource and the delegate for the UIPickerView. UITextField needs to be added as a subview on your UIViewController's view.

- (void)viewDidLoad {

    [super viewDidLoad];

    UIPickerView *picker = [[UIPickerView alloc] init];
    [picker setDataSource:self];
    [picker setDelegate:self];

    self.dummyTextField = [UITextField new];
    self.dummyTextField.inputView = picker;
    [self.dummyTextField setHidden:YES];
    [self.view addSubview:self.dummyTextField];
}

Finally, add a mechanism to show and hide the UIPickerView. Since I am using a dummy UITextField, I have decided to add a UIBarButtonItem named "Filter" with the following IBAction:

- (IBAction)tappedFilterButton:(UIBarButtonItem *)sender {

    if (self.dummyTextField.isFirstResponder) {

        [self.dummyTextField resignFirstResponder];
    }
    else {

        [self.dummyTextField becomeFirstResponder];
    }
}
like image 5
Yunus Nedim Mehel Avatar answered Nov 17 '22 02:11

Yunus Nedim Mehel