Search code examples
objective-ciosxcodeuipickerviewuiactionsheet

UIActionSheet comes up halfway


I was looking for a way to make a UIPickerView show a "Done", "Cancel" buttons.
Googling around I found good posts, all of them had UIActionSheet involved.

So I did that

container is a UIView
actionSheet is a UIActinSheet
pickerView is a UIPickerView

in viewDidLoad:

container = [[[UIView alloc] initWithFrame:CGRectMake(rect.origin.x,rect.origin.y, rect.size.width, rect.size.height)] autorelease];
[self.view addSubview:container];

actionSheet = [[UIActionSheet alloc] initWithTitle:@"myTitle" 
                                          delegate:nil
                                 cancelButtonTitle:nil
                            destructiveButtonTitle:nil
                                 otherButtonTitles:nil];

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

pickerView = [[[UIPickerView alloc] initWithFrame:pickerFrame]autorelease];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;

UIToolbar *toolbar = [[[UIToolbar alloc] init] autorelease];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[[NSMutableArray alloc] init] autorelease];
[items addObject:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dismissActionSheet)] autorelease]];
[items addObject:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(goActionSheet)] autorelease]];
[items addObject:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(goActionSheet)] autorelease]];
[toolbar setItems:items animated:NO];

[actionSheet addSubview:toolbar];
[actionSheet addSubview:pickerView];

[actionSheet showInView:container];

[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

The app starts great, and the whole thing shows up nice as supposed to be on the line: [actionSheet showInView:container];

Now, When I try to dismiss the ActionSheet, it works great!

in dismissActionSheet:

[actionSheet dismissWithClickedButtonIndex:1 animated:NO];

The problem is, how do I make it show up again?

When I use:

[actionSheet showInView:container];

I only get the Toolbar picking from the bottom with the buttons and that's it. The whole PickerView is at the bottom and I can't see it.

What am I doing wrong here?


Solution

  • Action sheets are intended to be used more like alerts, in that they are standalone "make a choice now" presenters. They do normally only display part-way on the iPhone.

    Your picker should be independent of the action sheet. If you want to have "Done" and "Cancel" buttons perhaps you would want to consider adding them to the picker's navigation bar?