ios - UIPickerView 完成按钮不起作用

标签 ios objective-c

我有一个 UITextField,我在上面放了 UIPickerView。当用户点击 textfield 时,pickerview 会像键盘一样出现。我试图借助工具栏中的“完成”按钮删除 pickerview,但它没有这样做。下面是我的代码。

我曾尝试在网上寻找想法,但没有解决我的目的。谁能帮我解决这个问题,就像我在这里所缺少的一样。

-(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;
    }
}

解决方案

作为临时解决方案,我使用了点击手势,因此当用户从选择器 View 中选择并点击屏幕时,选择器 View 消失了。下面是我的代码: 内部 viewDidLoad

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

然后

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

我仍然很想解开完成按钮不隐藏我的 pickerview 背后的谜团。

最佳答案

试着打电话

[self.view endEditing:YES];

-(void)hidePicker

关于ios - UIPickerView 完成按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27564346/

相关文章:

iphone - 多任务处理 : Stop Background Audio at Specific Time

iphone - 当没有互联网连接时无法计算 ScrollView 的高度

ios - 错误 Json : "NSDebugDescription Garbage at end" (iOS)

ios - 如何从V2 Box iOS SDK获取文件扩展名和路径?

ios - 如何在 UIScrollView 中实现缩放图像?

iphone - iOS应用启动或切换回前台的速度非常慢

ios - sql select 语句不起作用

iphone - 如果 iAd 未加载,如何显示错误消息?

ios - 为什么 Autolayout 约束可能不会在运行时出现?

ios - 如何从共享扩展中的主容器项目导入文件?