简体   繁体   English

UIPickerView完成按钮不起作用

[英]UIPickerView Done button not working

I have a UITextField on which I have put UIPickerView. 我有一个UITextField,上面放了UIPickerView。 When user click on textfield, pickerview will show up just like a keyboard. 当用户单击文本字段时,pickerview会像键盘一样显示。 I am trying to remove the pickerview with the help of Done button in it's toolbar but it's not doing so. 我正在尝试通过工具栏中的“完成”按钮删除pickerview,但是没有这样做。 Below is my code. 下面是我的代码。

I have tried looking for ideas online, but didn't solve my purpose. 我曾尝试过在线寻找想法,但没有解决我的目的。 Can anyone help me out with this, like what I am missing here. 任何人都可以帮我解决这个问题,就像我在这里想念的一样。

-(void)pickerViewSetUp
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    NSLog(@"%f %f",screenWidth,screenHeight);

    _myPickerView = [[UIPickerView alloc]init];
    _myPickerView.delegate = self;
    _myPickerView.dataSource = self;

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 44)];
    toolBar.barStyle = UIBarStyleBlackTranslucent;
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(hidePicker)];
    UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [toolBar setItems:[NSArray arrayWithObjects:flexible,doneButton, nil]];
    [_myPickerView addSubview:toolBar];
}

-(void)hidePicker
{
    [_toss resignFirstResponder];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField;
{
    if (textField == _toss)
    {
        _toss.inputView = _myPickerView;
    }
}

Solution

As a temp solution I have used a tap gesture, so when a user selects from picker view and taps on screen, pickerview is gone. 作为临时解决方案,我使用了点击手势,因此,当用户从选择器视图中选择并在屏幕上点击时,pickerview消失了。 Below is my code: Inside viewDidLoad 下面是我的代码:viewDidLoad内部

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidePicker)];
[self.view addGestureRecognizer:tap];

and then 接着

-(void)hidePicker
{
    [_toss resignFirstResponder];
}

I would still love to solve the mystery behind done button not hiding my pickerview. 我仍然很想解决完成按钮背后的奥秘,不要隐藏我的pickerview。

Try calling 尝试致电

[self.view endEditing:YES];

in -(void)hidePicker -(void)hidePicker

Not getting what you actually want to do. 没有得到您真正想要做的。 Just to remove that picker you Can You try : 只是要删除该选择器,您可以尝试:

[_myPickerView removeFromSuperview];

in -(void)hidePicker -(void)hidePicker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM