现在的位置: 首页 > 综合 > 正文

Hiding/ Showing UIPickerView

2013年10月01日 ⁄ 综合 ⁄ 共 1750字 ⁄ 字号 评论关闭

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;

}

share|improve
this question
   

3 Answers

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

if (pickerView) pickerView.hidden = !pickerView.hidden;
share|improve
this answer
 
1  
Crude, but works. – Kyle Sep
4 '12 at 22: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

抱歉!评论已关闭.