iOS / Objective-C: добавить метку для окна выбора в таблице действий

Я пытаюсь добавить ярлык к окну выбора, который находится в таблице действий.

Код работает, если окна выбора нет в таблице действий.

Мой код:

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

person Community    schedule 21.08.2012    source источник
comment
Не делай этого. Это то, что вам следует сделать   -  person bbarnhart    schedule 21.08.2012


Ответы (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, чтобы справиться с этим за вас. Это действительно упрощает работу с UIPicker / DatePickers в UIActionSheets.

Как видно из их 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];

Удачи!

person ebandersen    schedule 24.06.2013