如何让我的选择器正常工作?

时间:2021-11-07 19:31:20

I have been reading up on pickers and wanted to try and get a simple one working, but I do not know what I am doing wrong with my code.

我一直在阅读拾取器,并想尝试让一个简单的工作,但我不知道我的代码我做错了什么。

I have assigned my pickers in my storyboard to the correct controllers. I then added the following to my header.

我已经在我的故事板中将我的选择器分配给了正确的控制器。然后我将以下内容添加到我的标题中。

@interface CustomerView : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
{
    IBOutlet UIPickerView * CompanyPicker;
IBOutlet UIPickerView * ClientPicker;
    NSMutableArray *arrayCompanies;
NSMutableArray *arrayClients;
}

I then added some info to the arrays I created in my heading under didload:

然后我将一些信息添加到我在didload下的标题中创建的数组中:

  arrayCompanies = [[NSMutableArray alloc] init];
  [arrayCompanies addObject:@"Test Company 01"];
  [arrayCompanies addObject:@"Test Company 02"];
  [arrayCompanies addObject:@"Test Company 03"];
  [arrayCompanies addObject:@"Test Company 04"];
  [arrayCompanies addObject:@"Test Company 05"];
  [arrayCompanies addObject:@"Test Company 06"];
  [arrayCompanies addObject:@"Test Company 07"];

  arrayClients   = [[NSMutableArray alloc] init];
  [arrayClients addObject:@"Test Client 01"];
  [arrayClients addObject:@"Test Client 01"];
  [arrayClients addObject:@"Test Client 01"];
  [arrayClients addObject:@"Test Client 01"];
  [arrayClients addObject:@"Test Client 01"];
  [arrayClients addObject:@"Test Client 01"];
  [arrayClients addObject:@"Test Client 01"];

After that I added the following in the implementation, but the info is not seeming to load into my arrays. Am I doing something wrong here?

之后我在实现中添加了以下内容,但信息似乎没有加载到我的数组中。我在这里做错了吗?

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {

  return 2;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {

return [arrayCompanies count];
  return [arrayClients count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

return [arrayCompanies objectAtIndex:row];
    return [arrayClients objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

NSLog(@"Selected Company: %@. Index of selected company: %i", [arrayCompanies objectAtIndex:row], row);
  NSLog(@"Selected Client: %@. Index of selected client: %i", [arrayClients objectAtIndex:row], row);
}

1 个解决方案

#1


2  

use this technique:

使用这种技术:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {

  return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
if ([thePickerView isEqual: CompanyPicker]) {
 return [arrayCompanies count];
} else if ([thePickerView isEqual: ClientPicker]) {
  return [arrayClients count];
}
 return 0;

}

#1


2  

use this technique:

使用这种技术:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {

  return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
if ([thePickerView isEqual: CompanyPicker]) {
 return [arrayCompanies count];
} else if ([thePickerView isEqual: ClientPicker]) {
  return [arrayClients count];
}
 return 0;

}