UIActionSheet + UIPicker Example

1 ) Which thing you have to declare in .h file ??
Ans :

UIPickerView *pickerView;
UIToolbar *pickerToolbar;
UIActionSheet *pickerViewPopup;
NSMutableArray *ary; ( array which you want to fill into uipickerview )

2 ) How can you open and close actionsheet ?
Ans :
just only two functions you need to implement is
– (void) showPicker – this is used to open actionsheet with picker
-(BOOL)closePicker:(id)sender – this is used to close actionsheet with picker.

>>> Implementation of showPicker function

– (void) showPicker {
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:@”How many?”
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];

// Add the picker
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,44,0,0)];

pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES; // note this is default to NO

pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];

NSMutableArray *barItems = [[NSMutableArray alloc] init];

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];

UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(closePicker:)];
[barItems addObject:doneBtn];

[pickerToolbar setItems:barItems animated:YES];

[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];

}

>>>> Implementation of ClosePicker Function

-(BOOL)closePicker:(id)sender
{
[pickerViewPopup dismissWithClickedButtonIndex:0 animated:YES];
[pickerToolbar release];
[pickerViewPopup release];
return YES;
}

Advertisements

1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s