简体   繁体   English

iOS / Objective-C:在操作表中将标签添加到pickerview

[英]iOS / Objective-C: Add label to pickerview in actionsheet

I´m trying to add a label to a pickerview which is in an actionsheet. 我正在尝试向操作表中的pickerview添加标签。

The code is working if the pickerview is not in an actionsheet. 如果pickerview不在操作表中,则该代码有效。

My code: 我的代码:

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

You'll need to implement the following UIPickerView's datasource methods: 您需要实现以下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];
}

where self.pickerOptions is an Array of the 'label' values you're talking about. 其中self.pickerOptions是您所讨论的“标签”值的数组。 However, I would recommend using EAActionSheetPicker to handle this for you. 但是,我建议使用EAActionSheetPicker为您处理此问题。 It makes handling UIPicker/DatePickers in UIActionSheets really easy. 它使得在UIActionSheets中处理UIPicker / DatePickers非常容易。

As pulled from their README, it's implementation is as simple as: 从他们的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];

Best of luck! 祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM