我在 iOS 上实现 UIPickerView 时遇到了很多问题。我想显示一个选择器来填充某个字段,但仅在选择该字段时,并在其他情况下关闭(或隐藏)选择器。可能带有动画。
- 故事板:
我能够通过插座引用它,在我的 ViewController 上填充和显示它。但我无法使用 Storyboard 动态关闭(或隐藏) UIPickerView。我试过:
pickerView.hidden = YES;
但它没有用。有任何想法吗?
- 编码
我试图以编程方式实现我的 Picker:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
//if click on fieldWithPicker, dismiss the keyaboard and load the picker
if (textField == fieldWithPicker)
{
//dismiss the keyboard of fieldWithoutPicker
[fieldWithoutPicker resignFirstResponder];
// Check if the picker is already on screen. If so, skip creating picker view and go to handling choice
if (myPickerView.superview == nil)
{
//Make picker
myPickerView = [[UIPickerView alloc] initWithFrame:CGRectZero];
CGSize pickerSize = [myPickerView sizeThatFits:CGSizeZero];
myPickerView.frame = [self pickerFrameWithSize:pickerSize];
myPickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
myPickerView.showsSelectionIndicator = YES;
// this view controller is the data source and delegate
myPickerView.delegate = self;
myPickerView.dataSource = self;
// Add the picker
[self.view.window addSubview: myPickerView];
// size up the picker view to our screen and compute the start/end frame origin for our slide up animation
// compute the start frame
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGRect startRect = CGRectMake(0.0, screenRect.origin.y + screenRect.size.height, pickerSize.width, pickerSize.height);
mypickerView.frame = startRect;
// compute the end frame
CGRect pickerRect = CGRectMake(0.0, screenRect.origin.y + screenRect.size.height - pickerSize.height-100, pickerSize.width, pickerSize.height);
// start the slide up animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:PICKER_ANIMATION_DURATION];
// Give time for the table to scroll before animating the picker's appearance
[UIView setAnimationDelay:PICKER_ANIMATION_DELAY];
// we need to perform some post operations after the animation is complete
[UIView setAnimationDelegate:self];
myPickerView = pickerRect;
[UIView commitAnimations];
}
//we don't want keyboard, since we have a picker
return NO;
}
//if click on fieldWithoutPicker, dismiss the picker and load the keyboard
if (textField == fieldWithoutPicker)
{
[self hidePickerContinenteView];
}
return YES;
}
然后我实现hidePickerContinenteView
了移动到 Picker 之外的功能:
- (void) hidePickerContinenteView
{
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGRect endFrame = self.view.frame;
endFrame.origin.y = screenRect.origin.y + screenRect.size.height;
// start the slide down animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:PICKER_ANIMATION_DURATION];
// we need to perform some post operations after the animation is complete
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(slideDownDidStop)];
pickerContinenteView.frame = endFrame;
[UIView commitAnimations];
}
在这一点上,我有两个主要问题:
在该
textFieldShouldBeginEditing
方法中,我检查 PickerView 是否已经存在,以防万一,什么都不做。在hidePickerContinenteView
方法中我隐藏了 Picker,但是 PickerView 仍然存在:这意味着下次我点击 fieldWithPicker 时,Picker 不会显示出来。我尝试在hidePickerContinenteView
以下方法中使用:[pickerContinenteView removeFromSuperview];
但这会立即关闭 PickerView 并据此不显示动画。有任何想法吗?如果显示了 Picker 并且用户更改了视图,例如单击另一个选项卡或后退按钮,则 Picker 不会被关闭。对此还有什么想法吗?
提前谢谢你,亚萨