0

我正在尝试将标签添加到操作表中的选择器视图。

如果选择器视图不在操作表中,则代码正在工作。

我的代码:

- (void)initActionSheetPickerView {
  actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

  pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 0, 0)];
  pickerView.showsSelectionIndicator = YES;
  pickerView.dataSource = self;
  pickerView.delegate = self;
  pickerViewMeasureLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 97, 50, 22)];
  pickerViewMeasureLabel.text = @"asdf";
  pickerViewMeasureLabel.font = [UIFont boldSystemFontOfSize:20];
  pickerViewMeasureLabel.textColor = [UIColor blackColor];
  pickerViewMeasureLabel.backgroundColor = [UIColor clearColor];
  pickerViewMeasureLabel.shadowColor = [UIColor whiteColor];
  pickerViewMeasureLabel.shadowOffset = CGSizeMake (0, 1);
  [pickerView addSubview:pickerViewMeasureLabel];

  UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
  closeButton.momentary = YES;
  closeButton.frame = CGRectMake(260, 7, 50, 30);
  closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
  closeButton.tintColor = [UIColor blackColor];
  [closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];

  [actionSheet addSubview:pickerView];
  [actionSheet addSubview:closeButton];
}
4

1 回答 1

0

您需要实现以下 UIPickerView 的数据源方法:

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.pickerOptions count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [self.pickerOptions objectAtIndex:row];
}

其中 self.pickerOptions 是您正在谈论的“标签”值的数组。但是,我建议使用EAActionSheetPicker为您处理此问题。它使得在 UIActionSheets 中处理 UIPicker/DatePickers 变得非常容易。

从他们的 README 中提取,它的实现很简单:

NSArray *options = [NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", @"Five", nil];

EAActionSheetPicker *actionPicker = [[EAActionSheetPicker alloc]initWithOptions:options];
actionPicker.textField = self.emailField;
actionPicker.delegate = self;

[actionPicker showInView:self.view];

祝你好运!

于 2013-06-24T23:06:47.190 回答