iOS / Objective-C:在动作表中为pickerview添加标签

时间:2012-08-21 13:16:32

标签: objective-c ios uilabel uipickerview uiactionsheet

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

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

我的代码:

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

1 个答案:

答案 0 :(得分: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是您正在谈论的'label'值的数组。但是,我建议您使用EAActionSheetPicker来处理此问题。它使得在UIActionSheets中处理UIPicker / DatePickers非常容易。

从自述文件中提取,它的实现非常简单:

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

祝你好运!