ios - 一个 TableViewController 中的多个 UIPickerView 以编程方式

标签 ios xcode swift2 ios9 xcode7

几天来我一直在尝试这样做,但我找不到可行的方法。我尝试设置它的方式将 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/

相关文章:

ios - 自 iOS 9.0 起不再有可用位置

ios - 是否需要将项目迁移到 iOS 7?

ios - 如何访问 block 外部的变量?

ios - 如何获取 uibutton(UITableViewCell 的 subview )相对于整个窗口 iOS objective-C 的位置?

xcode - 为 UIPickerView 创建一个完成按钮

iphone - Unity iOS 版本 : app store size 4x ipa size

ios - 我可以使用自定义排序逻辑在 Swift 中对数组进行排序吗

ios - 如何通过发送产品名称来查找特定的产品 ID?

ios - UIDocumentInteractionController 不起作用

ios - 能够在越狱的 iOS 上运行未签名的应用程序