通过脚本或故事板动态关闭UIPickerView

时间:2011-12-11 14:42:31

标签: ios uipickerview storyboard picker dismiss

我在iOS上实现UIPickerView时遇到很多问题。 我想显示一个Picker来填充某个字段,但仅在选择字段时,并在其他情况下解除(或隐藏)Picker。可能是动画。

  • 故事板:

我能够通过插座引用它,填充并在我的ViewController上显示它。但我无法使用Storyboard动态解散(或隐藏)UIPickerView。 我试过了:

pickerView.hidden = YES;

但它不起作用。有什么想法吗?


  • CODING

我尝试以编程方式实现我的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,并根据此显示没有动画。 有什么想法吗?

  • 如果显示选择器并且用户更改了视图,例如单击另一个选项卡或后退按钮,则不会解除选择器。还有什么想法吗?

提前谢谢你, yassa

1 个答案:

答案 0 :(得分:0)

我会通过在故事板中创建和放置选择器来实现此目的,当它可见并使用时,您希望它在那里。检查属性检查器中的隐藏框,以便在加载视图时隐藏它。确保它位于故事板中的任何其他视图之上。然后编写视图控制器代码以使用隐藏属性显示它并再次隐藏。您仍然可以使用动画来更改隐藏属性。

隐藏选择器使用:

[templatePicker setHidden:YES];

并显示选择器的使用:

[templatePicker setHidden:NO];