ios7 - UIPickerView 工具栏按钮不起作用

标签 ios7 uipickerview

我正在尝试将工具栏添加到 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 Action 有关)
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。

最佳答案

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

确保实现接口(interface) 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];

关于ios7 - UIPickerView 工具栏按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20053405/

相关文章:

iphone - iOS 7 UINavigationBar - UIView 布局问题

ios - 该应用在 Payload/<AppName>.app/<AppName> 中引用非公共(public)选择器

ios - UIPickerView "didSelectRow"选择带有指向 url 的按钮

iphone - 如何在使用 UIPickerView 时禁用 View

ios - 在操作表中添加 UIPickerView 和一个按钮 - 如何?

ios - 快速重新加载 PickerView 的特定行

objective-c - 如何抑制 Master-Detail Controller 滑动手势,并在 iOS 7 上重新创建其行为

ios - 为什么 AVPlayer 不停止加载数据?

iphone - iOS 7 蓝牙 - 即使手机重启后也能在后台处理事件的应用程序

uipickerview - iOS 7.1 UITapGesture 不适用于 UIPickerView