UIPickerView 工具栏按钮不起作用

Posted

技术标签:

【中文标题】UIPickerView 工具栏按钮不起作用【英文标题】:UIPickerView toolbar buttons not working 【发布时间】:2013-11-18 16:59:23 【问题描述】:

我正在尝试将工具栏添加到UIPicker。 我已经看到正确的方法是这样:

UIToolbar* toolbarWeight= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *barButtonDone1 = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                               style:UIBarButtonItemStyleBordered target:self action:@selector(gotoNextField:)];
[toolbarWeight setItems:[NSArray arrayWithObject:barButtonNext1] animated:YES]; 

weightPickerView = [[UIPickerView alloc]init];
[weightPickerView addSubview:toolbarWeight];

同时:

-(IBAction)gotoNextField:(id)sender
    NSLog(@"Works");

选择器工作正常,但按钮不起作用。 在做了一些研究之后,我尝试了这种方法: (我怀疑问题与 UIBarButtonItem 动作有关)

UIButton* myBtn = [[UIButton alloc]init];
myBtn.frame = CGRectMake(0,0,50,24);
[myBtn addTarget:self action:@selector(gotoNextField:) forControlEvents:UIControlEventTouchUpInside];
[myBtn setTitle:@"Next!" forState:UIControlStateNormal];
[myBtn setBackgroundColor:[UIColor orangeColor]];
UIBarButtonItem *barButtonNext1 = [[UIBarButtonItem alloc] initWithCustomView:myBtn];
[toolbarWeight setItems:[NSArray arrayWithObject:barButtonNext1] animated:YES];

也不行。该按钮出现但没有被选中/响应 touchUpInside。

【问题讨论】:

“不起作用”是指您的按钮出现在屏幕上,但是当您点击它时没有任何反应? 没错。我将编辑问题。 【参考方案1】:

这里是添加包含工具栏的 UIPickerView 的示例 - 适用于 iOS 7

确保实现 UIPickerViewDataSource、UIPickerViewDelegate 接口并实现 @selector 方法

pickerView = [[UIPickerView alloc] init];
[pickerView setDataSource: self];
[pickerView setDelegate: self];
pickerView.showsSelectionIndicator = YES;

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(itemWasSelected:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(logoutData)];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithTitle:@"Clear" style:UIBarButtonItemStyleBordered target:self action:@selector(clearData)];
[toolBar setItems:[NSArray arrayWithObjects:cancelButton, clearButton, flexible, doneButton, nil]];

pickerParentView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, 320, 260)];
[pickerParentView addSubview:pickerView];
[pickerParentView addSubview:toolBar];
[self.view addSubview:pickerParentView];

【讨论】:

【参考方案2】:

当向pickerView 添加工具栏时,工具栏被添加到picker 后面,防止按钮响应触摸事件。

将选取器和工具栏都封装在一个视图中,并使其成为 inputView(使选取器和工具栏同时弹出)提供了所需的解决方案。

weightPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 44, 320, 216)];
weightPickerView.tag = 13;
UIView* genericWeightView = [[UIView alloc]initWithFrame:CGRectMake(0, 219, 320, 266)];
[genericWeightView addSubview:toolbarWeight];
[genericWeightView  addSubview:weightPickerView];

然后:

[weight_txtField setInputView:genericWeightView];

【讨论】:

以上是关于UIPickerView 工具栏按钮不起作用的主要内容,如果未能解决你的问题,请参考以下文章

UIPickerview 完成按钮不起作用,即使它没有调用选择器方法

UIPickerView 重新加载组件不起作用

用UIPickerView来显示省和市

UIPickerView 在 IOS 8 中不起作用

UIPickerView 选择行不起作用 ios7

将 Json 数组传递给 UIPickerView 不起作用