I have a screen with many text fields on it. For one particular text field, I need it to show a picker from which the user can pick state names, once the user has picked a state by clicking on the states name, the picker should disappear.
I am not quite sure how to do this, I have made the Nib file which looks like this.
And here is my header file:
#import <UIKit/UIKit.h> #import "APIHelper.h" @interface AddNewAddressViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate> { APIHelper *myAPIHelper; NSMutableData *responseData; NSURLConnection *theConnection; UIPickerView *picker; NSArray *stateNames; @public IBOutlet UIImageView *imageNewAddressBox; @public IBOutlet UIImageView *imageNewAddressBox1; @public IBOutlet UIImageView *imageNewAddressBox2; @public IBOutlet UIImageView *imageNewAddressBox3; @public IBOutlet UILabel *labelNewAddress; @public IBOutlet UILabel *labelStreetAddress; @public IBOutlet UILabel *labelStreetAddressTwo; @public IBOutlet UILabel *labelZipCode; @public IBOutlet UILabel *labelState; @public IBOutlet UILabel *labelCity; @public IBOutlet UILabel *labelPhoneNumber; @public IBOutlet UITextField *textFieldFullName; @public IBOutlet UITextField *textFieldStreetAddress; @public IBOutlet UITextField *textFieldPhoneNumber; @public IBOutlet UITextField *textFieldStreetAddressTwo; @public IBOutlet UITextField *textFieldCity; @public IBOutlet UITextField *textFieldState; @public IBOutlet UITextField *textFieldZipCode; @public IBOutlet UIButton *buttonNext; } @property (nonatomic, strong) IBOutlet UIPickerView *picker; @property (nonatomic, strong) NSArray *stateNames; @property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox; @property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox1; @property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox2; @property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox3; @property (nonatomic, strong) IBOutlet UILabel *labelNewAddress; @property (nonatomic, strong) IBOutlet UILabel *labelStreetAddress; @property (nonatomic, strong) IBOutlet UILabel *labelStreetAddressTwo; @property (nonatomic, strong) IBOutlet UILabel *labelZipCode; @property (nonatomic, strong) IBOutlet UILabel *labelState; @property (nonatomic, strong) IBOutlet UILabel *labelCity; @property (nonatomic, strong) IBOutlet UILabel *labelPhoneNumber; @property (nonatomic, strong) IBOutlet UITextField *textFieldFullName; @property (nonatomic, strong) IBOutlet UITextField *textFieldStreetAddress; @property (nonatomic, strong) IBOutlet UITextField *textFieldPhoneNumber; @property (nonatomic, strong) IBOutlet UITextField *textFieldStreetAddressTwo; @property (nonatomic, strong) IBOutlet UITextField *textFieldCity; @property (nonatomic, strong) IBOutlet UITextField *textFieldState; @property (nonatomic, strong) IBOutlet UITextField *textFieldZipCode; @property (nonatomic, strong) IBOutlet UIButton *buttonNext; -(IBAction)addressTextFieldShouldReturn:(id)sender; -(IBAction)nextPressed; @end
And here is my .M file:
#import "AddNewAddressViewController.h" @implementation AddNewAddressViewController @synthesize imageNewAddressBox; @synthesize imageNewAddressBox1; @synthesize imageNewAddressBox2; @synthesize imageNewAddressBox3; @synthesize labelNewAddress; @synthesize labelStreetAddress; @synthesize labelStreetAddressTwo; @synthesize labelZipCode; @synthesize labelState; @synthesize labelCity; @synthesize labelPhoneNumber; @synthesize textFieldFullName; @synthesize textFieldStreetAddress; @synthesize textFieldPhoneNumber; @synthesize textFieldStreetAddressTwo; @synthesize textFieldCity; @synthesize textFieldState; @synthesize textFieldZipCode; @synthesize buttonNext; @synthesize stateNames; @synthesize picker; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { self.stateNames = [[NSArray alloc] initWithObjects:@"Alabama", @"Alaska", @"Arizona", @"Arkansas", @"California", @"Colorado", @"Connecticut", @"Delaware", @"District of Columbia", @"Florida", @"Georgia", @"Guam", @"Hawaii", @"Idaho", @"Illinois", @"Indiana", @"Iowa", @"Kansas", @"Kentucky", @"Louisiana", @"Maine", @"Maryland", @"Massachusetts", @"Michigan", @"Minnesota", @"Mississippi", @"Missouri", @"Montana", @"Nebraska", @"Nevada", @"New Hampshire", @"New Jersey", @"New Mexico", @"New York", @"North Carolina", @"North Dakota", @"Ohio", @"Oklahoma", @"Oregon", @"Pennsylvania", @"Puerto Rico", @"Rhode Island", @"South Carolina", @"South Dakota", @"Tennessee", @"Texas", @"Utah", @"Vermont", @"Virginia", @"Virgin Islands", @"Washington", @"West Virginia", @"Wisconsin", @"Wyoming", nil]; [super viewDidLoad]; // Do any additional setup after loading the view from its nib. // clicking outside the textfields removes keyboard UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addressTextFieldShouldReturn:)]; [self.view addGestureRecognizer:tap]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self setTitle: @"Update Address Book"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)viewDidUnload { [super viewDidUnload]; } -(IBAction)addressTextFieldShouldReturn:(id)sender { [textFieldFullName resignFirstResponder]; [textFieldStreetAddress resignFirstResponder]; [textFieldStreetAddressTwo resignFirstResponder]; [textFieldPhoneNumber resignFirstResponder]; [textFieldState resignFirstResponder]; [textFieldCity resignFirstResponder]; [textFieldZipCode resignFirstResponder]; [sender resignFirstResponder]; } #pragma mark - #pragma mark PickerView DataSource - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [stateNames count]; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component { return [stateNames objectAtIndex:row]; } @end
So, all I want it do is, when the user clicks on the textFieldState, a picker with all of the states name in it pops up from the bottom and then the user clicks on the states name and that name appears in textFieldState.
Any help would be greatly appreciated. Thanks.