Ios 通过脚本或情节提要动态关闭UIPickerView
在iOS上实现UIPickerView时,我面临许多问题。 我希望显示一个选择器来填充某个字段,但仅当该字段被选中时,并且在其他情况下关闭(或隐藏)选择器。可能是动画Ios 通过脚本或情节提要动态关闭UIPickerView,ios,uipickerview,storyboard,picker,dismiss,Ios,Uipickerview,Storyboard,Picker,Dismiss,在iOS上实现UIPickerView时,我面临许多问题。 我希望显示一个选择器来填充某个字段,但仅当该字段被选中时,并且在其他情况下关闭(或隐藏)选择器。可能是动画 情节提要: 我可以用插座引用它,填充它并在我的ViewController上显示它。但我无法用情节提要动态地消除(或隐藏)UIPickerView。 我试过: pickerView.hidden = YES; 但它不起作用。有什么想法吗 编码 我尝试以编程方式实现我的选择器: - (BOOL)textFieldShou
- 情节提要:
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;
}
然后,我实现了我的hiddeview
功能,以移动到选择器之外:
- (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];
}
在这一点上,我有两个主要问题:
- 在
方法中,我检查PickerView是否已经存在,以防什么也不做。在textFieldShouldBeginEditing
方法中,我隐藏了选取器,但选取器视图仍然存在:这意味着下次单击fieldWithPicker时,不会显示选取器。 我尝试在hidepickereview
方法中使用以下内容:hideview
[PickerContainetView从SuperView移除]代码> 但是,这会立即关闭PickerView,并且根据这一点,不会显示任何动画。 有什么想法吗
- 如果显示选择器,并且用户更改了视图,例如单击另一个选项卡或“后退”按钮,则不会解除选择器。你对此也有什么想法吗
yassa我将通过创建选择器并将其放置在故事板中,当它可见并使用时,您希望它位于何处来实现这一点。选中“属性检查器”中的“隐藏”框,以便在加载视图时将其隐藏。确保它位于故事板中任何其他视图的顶部。然后对视图控制器进行编码,以使用隐藏属性来显示视图控制器,然后再次使用隐藏属性来隐藏视图控制器。仍然可以使用动画来更改隐藏特性 要隐藏选择器,请使用:
[templatePicker setHidden:YES];
[templatePicker setHidden:NO];
并显示选择器的使用:
[templatePicker setHidden:YES];
[templatePicker setHidden:NO];
关于第一个问题,这很有魅力,谢谢!!!但是,如果用户通过单击另一个选项卡(例如)来更改视图,则选择器将保持可见。如何拦截此事件?请注意,
textFieldShouldEndEditing
事件不会触发,因为在textFieldShouldStartEditing
上,我必须返回“否”以避免键盘出现。我无法理解该行为。如果您仅使用故事板创建选择器,当您从包含选择器的视图切换到另一个视图时,它应该消失。是否可以在此选择器上显示动画?我的意思是:我把我的选择器放在故事板上看不见的地方。然后对于某个事件,我将其设置为在某个位置可见,然后将其移动到另一个位置。。。