1

I am implementing picker by Add UIPickerView & a Button in Action sheet - How?. I tried to replace the UISegmentedControl with a UIButton but it does not working. Could someone remind me why UIButton cannot be shown here?

Thank you.

- (IBAction) makeButtonPressed: (id) sender;
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] init];
    [actionSheet setTitle:@"Select Make"];
    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackOpaque];

    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
    pickerView.dataSource = self;
    pickerView.delegate = self;
    pickerView.showsSelectionIndicator = YES;
    [actionSheet addSubview:pickerView];

//  UISegmentedControl *doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
//  doneButton.momentary = YES; 
//  doneButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
//  doneButton.segmentedControlStyle = UISegmentedControlStyleBar;
//  doneButton.tintColor = [UIColor blackColor];
//  [doneButton addTarget:self action:@selector(makeDone:) forControlEvents:UIControlEventValueChanged];
//  [actionSheet addSubview:doneButton];

    UIButton *doneButton = [[UIButton alloc] init];
    doneButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
    [doneButton addTarget:self action:@selector(makeDone:) forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:doneButton];

    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

    [actionSheet setBounds:CGRectMake(0, 0, 320, 460)];
}
Community
  • 1
  • 1
ThinkChris
  • 1,169
  • 2
  • 15
  • 38

1 Answers1

3

The designated initializer for UIButton is +buttonWithType:. You should create your button with one of the predefined styles, or use UIButtonTypeCustom.

UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeRoundRect];
doneButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
[doneButton addTarget:self action:@selector(makeDone:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:doneButton];
Mark Adams
  • 30,776
  • 11
  • 77
  • 77