Some time it is required in iPhone to add UIPickerView in UIActionSheet. Here are the steps to do that
In ViewController.m
In ViewController.h
@interface ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate> |
{ |
IBOutlet UITextField *txtText; |
NSArray *arrState; |
UIPickerView *pktStatePicker ;
UIToolbar *mypickerToolbar;} |
In ViewController.m
@implementation ViewController |
- ( void )viewDidLoad |
{ |
[ super viewDidLoad]; |
arrState= [ NSArray initWithObjects: @"NewYork" , @"NewJercy" , @"Carlifornia" , @"Florida" , @"Fremont" , @"SantaClara" , @"San Diego" , @"San Fransisco" , @"San Jose" , nil ]; |
pktStatePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 43, 320, 480)]; |
pktStatePicker.delegate = self ; |
pktStatePicker.dataSource = self ; |
[pktStatePicker setShowsSelectionIndicator: YES ]; |
txtText.inputView = pktStatePicker ; |
// Create done button in UIPickerView |
mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)]; |
mypickerToolbar.barStyle = UIBarStyleBlackOpaque; |
[mypickerToolbar 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 (pickerDoneClicked)]; |
[barItems addObject:doneBtn]; |
[mypickerToolbar setItems:barItems animated: YES ]; |
txtText.inputAccessoryView = mypickerToolbar; |
} |
-( void )pickerDoneClicked |
{ |
NSLog ( @"Done Clicked" ); |
[txtText resignFirstResponder]; |
mypickerToolbar.hidden=YES;
pktStatePicker.hidden=YES;
} |
- ( NSInteger )numberOfComponentsInPickerView:(UIPickerView *)pickerView |
{ |
return 1; |
} |
- ( NSInteger )pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:( NSInteger )component |
{ |
return [arrState count]; |
} |
- ( NSString *)pickerView:(UIPickerView *)pickerView titleForRow:( NSInteger )row forComponent:( NSInteger )component |
{ |
return [arrState objectAtIndex:row]; |
} |
- ( void ) pickerView:(UIPickerView *)pickerView didSelectRow:( NSInteger )row inComponent:( NSInteger )component |
{ |
txtText.text = [arrState objectAtIndex:row]; |
} |
4 comments:
Great Demo, thanks!
One Issue, When I click the Done button, then click into the text field again, a grey bar comes up where the pickerView should be but the pickerView and the toolbar are missing.
I found that if I just removed these two lines of code from the pickerDoneClicked method, it worked properly.
-(void)pickerDoneClicked
{
NSLog(@"Done Clicked!");
[txtText resignFirstResponder];
// mypickerToolbar.hidden = YES; // remove this line
// pktStatePicker.hidden = YES; // remove this line
}
on loading newyork if clicking done button it is not shown in textfield.....how to do this in initial loading...
initial loading ...on picker view...newyork is loaded...during that time done button is clicked it won't get the value......after scrolling it works fine how to correct it on initial...loading sir...
Post a Comment