通过脚本或 Storyboard 动态关闭 UIPickerView
我在 iOS 上实现 UIPickerView 时遇到很多问题。 我想显示一个选择器来填充某个字段,但仅当选择该字段时才显示,并在其他情况下关闭(或隐藏)选择器。可能有动画。
- 故事板:
我能够通过插座引用它,填充并在我的 ViewController 上显示它。但我无法使用 Storyboard 动态关闭(或隐藏) UIPickerView。 我尝试过:
pickerView.hidden = YES;
但没有成功。有什么想法吗?
- 编码
我尝试以编程方式实现我的选取器:
- (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
函数以移出选取器:
- (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 并且根据此,不会显示任何动画。 有什么想法吗?如果显示选取器并且用户更改视图,例如单击另一个选项卡或后退按钮,则不会关闭选取器。对此还有什么想法吗?
先感谢您, 亚萨
I'm facing many problems in implementing UIPickerView on iOS.
I would like to show a Picker to populate a certain field, but only when the field is selected, and dismiss (or hide) the Picker in other cases. Possibly with an animation.
- STORYBOARD:
I was able to reference it with an outlet, populate and show it on my ViewController. But I wasn't able to dynamically dismiss (or hide) the UIPickerView with Storyboard.
I've tried with:
pickerView.hidden = YES;
but it didn't work. Any ideas?
- CODING
I tried to implement my Picker programmatically:
- (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;
}
Then I've implemented my hidePickerContinenteView
function to move outside the 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];
}
At this point I've two main problems:
In the
textFieldShouldBeginEditing
method, I check it the PickerView already exists and in case, nothing is done. In thehidePickerContinenteView
method I hide the Picker, but the PickerView continues to exist: this means that the next time I click on fieldWithPicker, the Picker is not displayed.
I've tried using in thehidePickerContinenteView
method the following:[pickerContinenteView removeFromSuperview];
but this dismiss immediately the PickerView and according to this, no animation is displayed.
Any ideas?If the Picker is displayed and the user changes the view, for example clicking on another tab or on the back button, the Picker is not dismissed. Any ideas also on this?
Thank you in advance,
yassa
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将通过创建选择器并将其放置在故事板中您希望它可见和使用时所在的位置来解决此问题。选中属性检查器中的隐藏框,以便在加载视图时它将隐藏。确保它位于故事板中任何其他视图的顶部。然后对视图控制器进行编码以使用隐藏属性来显示它并再次隐藏。您仍然可以使用动画来更改隐藏属性。
要隐藏选择器,请使用:
并显示选择器,请使用:
I would approach this by creating and placing the picker in storyboard where you want it to be when it is visible and used. Check the hidden box in the attribute inspector so it will be hidden when the view loads. Make sure it is on top of any other view you have in storyboard. Then code your view controller to use the hidden property to show it and again to hide. You can still use animation for changing the hidden property.
To hide the picker use:
and to show the picker use: