【问题标题】:iOS / Objective-C: Add label to pickerview in actionsheetiOS / Objective-C:将标签添加到操作表中的pickerview
【发布时间】:2012-08-21 13:16:31
【问题描述】:

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

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

我的代码:

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

【问题讨论】:

标签: objective-c ios uilabel uipickerview uiactionsheet


【解决方案1】:

您需要实现以下 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];

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 2011-03-13
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多