几天来我一直在尝试这样做,但我找不到可行的方法。我尝试设置它的方式将 pickerviews 加载为文本字段的键盘,这是我想要的,但它没有显示我放入其中的任何数据。有人可以帮助我解决这个问题,或者给我一个链接和例子。感谢任何帮助者。
最佳答案
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar setBarStyle:UIBarStyleBlack];
[toolbar setBarTintColor:[UIColor colorWithRed:237.0/255.0 green:30.0/255.0 blue:36.0/255.0 alpha:1.0]];
[toolbar sizeToFit];
UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(resignKeyboard)];
doneButton.tintColor = [UIColor whiteColor];
NSArray *itemsArray = [NSArray arrayWithObjects:flexButton, doneButton, nil];
[toolbar setItems:itemsArray];
yourTextField.inputAccessoryView = toolbar;
UIPickerView *categoryPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 210, 320, 216)];
categoryPicker.backgroundColor = [UIColor whiteColor];
categoryPicker.delegate = self;
categoryPicker.dataSource = self;
categoryPicker.tag = 1;
yourTextField.inputView = categoryPicker;
在你的 viewdidappear 中写完所有这些代码后,实现你的 pickerview 的委托(delegate)和数据源
pragma mark -UIPickerView 委托(delegate)和数据源
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return yourDataSource.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [yourDataSource objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
yourTextField.text = [yourDataSource objectAtIndex:row];
}
并像那样为完成按钮实现一个选择器
- (void)resignKeyboard
{
[bloodGroupField resignFirstResponder];
}
关于ios - 一个 TableViewController 中的多个 UIPickerView 以编程方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32687870/