1

I have a textfield. once I tap on that I want a UIPickerView to pop out and select an item from the UIPickerView and after once I am done with the selection I want to confirm the selection by tapping on an UIActionSheet which has confirm button on it.So my requirement is to have a UIActionSheet and beneath that a picker view. I am just a beginner in iOS development and I am still not familiar with the technical terminologies. So please bear with my informal way of asking questions.

Here is my block of code:

creating UIPickerView and UIActionSheet

UIActionSheet *confirmRoomSelectionAS =[[UIActionSheet alloc]initWithTitle:@"" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Done",nil];

UIPickerView *roomTypePicker=[[UIPickerView alloc]init];

[confirmRoomSelectionAS addSubview:roomTypePicker];
[confirmRoomSelectionAS showInView:self.view];
[confirmRoomSelectionAS setBounds:CGRectMake(0, 0, 320, 600)];

[roomTypePicker setFrame:CGRectMake(0, 170, 320, 216)];
[roomTypePicker setDataSource:self];
[roomTypePicker setDelegate: self];
roomTypePicker.showsSelectionIndicator = YES;
icodebuster
  • 8,890
  • 7
  • 62
  • 65
Francis S
  • 183
  • 3
  • 12

2 Answers2

2

Try like this,

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[actionSheet showInView:[self.view window]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 390)];
[actionSheet addSubview:[self createToolbar:@"Test"]];

UIPickerView *picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 320, 162);
picker.showsSelectionIndicator = YES;
picker.dataSource = self;
picker.delegate = self;
[actionSheet addSubview:picker];

- (UIView *)createToolbar:(NSString *)titleString {

    UIToolbar *inputAccessoryView = [[UIToolbar alloc] init];
    inputAccessoryView.barStyle = UIBarStyleBlackOpaque;
    inputAccessoryView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [inputAccessoryView sizeToFit];
    CGRect frame = inputAccessoryView.frame;
    frame.size.height = 44.0f;
    inputAccessoryView.frame = frame;

    UIBarButtonItem *doneBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
    UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *cancelBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0 , 11.0f, 100, 21.0f)];
    [titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:14]];
    [titleLabel setBackgroundColor:[UIColor clearColor]];
    [titleLabel setTextColor:[UIColor whiteColor]];
    [titleLabel setText:titleString];
    [titleLabel setTextAlignment:UITextAlignmentCenter];

    UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];


    NSMutableArray *array = [NSMutableArray arrayWithObjects:cancelBtn,flexibleSpaceLeft,title,flexibleSpaceLeft, doneBtn, nil];
    [inputAccessoryView setItems:array];

    [doneBtn release];
    [title release];
    [titleLabel release];
    [flexibleSpaceLeft release];
    [cancelBtn release];

    return [inputAccessoryView autorelease];
}

- (void)done:(id)sender {
}

- (void)cancel:(id)sender {
}

It will be like,

enter image description here

Venk
  • 5,949
  • 9
  • 41
  • 52
0

3 things I thought you should modify:

1) add your cancel button manually, to later better check the cancel button pressing event

UIActionSheet * confirmRoomSelectionAS = [[UIActionSheet alloc] 
                         initWithTitle:@"" 
                         delegate:self 
                         cancelButtonTitle:nil 
                         destructiveButtonTitle:nil 
                         otherButtonTitles:nil];
[confirmRoomSelectionAS addButtonWithTitle:@"Done"]; 
confirmRoomSelectionAS.cancelButtonIndex = [confirmRoomSelectionAS addButtonWithTitle: @"Cancel"];
[confirmRoomSelectionAS showInView:self.view];

2) implement your actionsheet delegate method

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == actionSheet.cancelButtonIndex) {
    // cancel button pressed;
        return;
    }
}

3) You should declare *UIPickerView picker in your .h to later check of its selected value in the previous delegate method (also including the Done check) by adding sthing like this:

NSInteger row;
row = [picker selectedRowInComponent:0];
nzs
  • 3,252
  • 17
  • 22