0

我在 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 不会被关闭。对此还有什么想法吗?

提前谢谢你,亚萨

4

1 回答 1

0

我会通过创建选择器并将其放置在情节提要中您希望它可见和使用时的位置来解决此问题。选中属性检查器中的隐藏框,以便在加载视图时将其隐藏。确保它位于情节提要中的任何其他视图之上。然后对您的视图控制器进行编码以使用 hidden 属性来显示它并再次隐藏它。您仍然可以使用动画来更改隐藏属性。

要隐藏选择器,请使用:

[templatePicker setHidden:YES];

并显示选择器使用:

[templatePicker setHidden:NO];
于 2011-12-11T15:27:05.447 回答