我正在尝试添加一个工具栏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];
Run Code Online (Sandbox Code Playgroud)
而:
-(IBAction)gotoNextField:(id)sender{
NSLog(@"Works");
}
Run Code Online (Sandbox Code Playgroud)
选择器工作正常,但按钮不起作用.做了一些研究之后,我尝试了这种方法:(我怀疑这个问题与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];
Run Code Online (Sandbox Code Playgroud)
也不起作用.该按钮出现但未被选中/响应touchUpInside.
Tho*_*ues 14
这里添加一个包含工具栏的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];
Run Code Online (Sandbox Code Playgroud)
在将工具栏添加到 pickerView 时,工具栏会添加到选取器后面,从而防止按钮响应触摸事件。
将选取器和工具栏封装在视图中,并将其设为 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];
Run Code Online (Sandbox Code Playgroud)
进而:
[weight_txtField setInputView:genericWeightView];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5638 次 |
| 最近记录: |