Hiding/ Showing UIPickerView

后端 未结 8 581
广开言路
广开言路 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:32

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

    if (pickerView) pickerView.hidden = !pickerView.hidden;
    
    0 讨论(0)
  • 2020-12-05 11:40

    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.

    0 讨论(0)
提交回复
热议问题