【问题标题】:Multiple UIPickerView in the one TableViewController Programatically一个 TableViewController 中的多个 UIPickerView 以编程方式
【发布时间】:2015-09-21 05:20:24
【问题描述】:

我这几天一直在尝试这样做,但我找不到可行的方法。我尝试设置它的方式将选择器视图加载为文本字段的键盘,这是我想要的,但它没有显示我放入其中的任何数据。有人可以帮助我解决这个问题,或者给我一个链接和例子。感谢任何帮助。

【问题讨论】:

    标签: ios xcode swift2 ios9 xcode7


    【解决方案1】:
        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 的委托和数据源

    pragma mark -UIPickerView 委托和数据源

    - (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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      相关资源
      最近更新 更多