具有不同值的多个Uipickerview

时间:2014-11-20 13:46:05

标签: objective-c iphone uipickerview

我有5个表,我必须为每个表创建一个UIPickerView。

我创建了一个,就像这样

我如何处理另外4张桌子?我必须制作此代码的4份副本吗?

我不知道我是否有正确的方法来创建我的UIPickerView,我想要确认(或不是)。 这就是我所做的(我从我的同事那里获取代码,同时也是初学者)

这是正确的方法吗?

如果我使用UIPickerViews 5,如何调整它?

@property (nonatomic, strong) UITextField *pickerViewDossier;

- (void)viewDidLoad {
[super viewDidLoad];

self.pickerViewDossier = [[UITextField alloc] initWithFrame:CGRectZero];
[self.view addSubview:self.pickerViewDossier];

UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;

self.pickerViewDossier.inputView = pickerView;

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Valider" style:UIBarButtonItemStyleDone target:self action:@selector(doneTouched:)];

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Annuler" style:UIBarButtonItemStyleDone target:self action:@selector(cancelTouched:)];

[toolBar setItems:[NSArray arrayWithObjects:cancelButton, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];

self.pickerViewDossier.inputAccessoryView = toolBar;

self.listeSuiviDossier = [NSArray arrayWithObjects:@"Vous-même", @"Un confrère",nil];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return listeSuiviDossier.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *titrePickerview = listeSuiviDossier[row];
return titrePickerview;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSString *titrePickerview = listeSuiviDossier[row];
[suiviDossierBouton setTitle:titrePickerview forState:UIControlStateNormal];
}

- (IBAction)suiviDossierAction:(id)sender {
[self.pickerViewDossier becomeFirstResponder];
}

- (void)cancelTouched:(UIBarButtonItem *)sender
{
[self.pickerViewDossier resignFirstResponder];
[suiviDossierBouton setTitle:@"Sélectionnez" forState:UIControlStateNormal];
}

- (void)doneTouched:(UIBarButtonItem *)sender
{
[self.pickerViewDossier resignFirstResponder];
}

1 个答案:

答案 0 :(得分:0)

使用标签进行选择,以便您可以选择为每个选择器执行的操作。

例如:

你宣布拣货员的地方:

picker1.tag = 1
picker2.tag = 2

并为每种方法:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) { 
    if(pickerView.tag == 1) // you can also use a switch
     {
          // picker 1
     }
     else if(pickerView.tag == 2)
     {
          // picker 2
     }
     else if(pickerView.tag == 3)
     {
          // picker 3
     }     
     else if(pickerView.tag == 4)
     {
          // picker 4
     }
}