title: Multiple UIPickerViews in IOS5 app
body: <p>I started learning iOS programming about two months ago and so far I love it, but I'm kinda struggling a bit with UIPicker(s) and UIDatePickers in the app that I'm making, and I was hoping that someone might point me in the right direction. The app is supposed to mimic already existing PHP web app, that is nothing more than a form with a bunch of dropdowns.</p>
  
  <p>Lets say that i have GetAddressViewController that will have a bunch of input fields. For the sake of simplicity lets say that I will have only 3 input fields for: country, city and street.</p>
  
  <p>The user should tap on "country" input field and the UIPicker shows up much like a keyboard would and after selecting the country, a web app will return a JSON array with all the cities of that country. The same process is repeated when user taps on "city" input field, the UIPicker pops up with a list of returned cities, user selects a street, the UIPicker slides out and web app returns an array of streets etc.</p>
  
  <p>Lets say that in my In my GetAddressViewController.h file i have:</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @interface GetAddressViewController : UIViewController
  {
      UITextField *countryField;
      UITextField *cityField;
      UITextField *streetField;
  
      NSArray *countries;
      NSArray *cities;
      NSArray *streets;
  }
  
  @property(nonatomic, strong) IBOutlet UITextField *countryField;
  @property(nonatomic, strong) IBOutlet UITextField *cityField;
  @property(nonatomic, strong) IBOutlet UITextField *streetField;
  
  @property(nonatomic, strong) IBOutlet NSArray *countries;
  @property(nonatomic, strong) IBOutlet NSArray *cities;
  @property(nonatomic, strong) IBOutlet NSArray *streets;
  
  @end
  </code></pre>
  
  <p>In GetAddressViewController.m file i have synthesized properties and in the storyboard i only have 3 input fields that have been connected to appropriate outlets.
  Is there some fundamental mistake in my existing code that i should be aware of? </p>
  
  <p>Now, I feel that I have missed something while reading the tutorials regarding picker views, since most of the examples that I've found on StackOverflow don't make much sense to me. </p>
  
  <p>Could someone point me to a basic similar example that could help me. Are UIPickers the way to go or is there a better approach? </p>
  
  <p>I haven't felt this helpless for a while and any help would be greatly appreciated, thanks</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Ok I'm making progress I hope that I can help someone else who has the same problem. 
  To make this work and to connect pickerview to input fileds inputView property you need to do this.</p>
  
  <p>To your .h file you need to add two delegates: UIPickerViewDelegate and UIPickerViewDataSource</p>
  
  <pre><code>@interface ViewController : UIViewController&lt;UIPickerViewDelegate, UIPickerViewDataSource&gt;{ ...
  </code></pre>
  
  <p>Then in your .m file you'll need to have something like this. I have this code in my wiewDidLoad method.</p>
  
  <pre><code>UIPickerView *cityPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
      cityPicker.delegate = self;
      cityPicker.dataSource = self;
      [cityPicker setShowsSelectionIndicator:YES];
      cityField.inputView = cityPicker;
  </code></pre>
  
  <p>This simply means that cityPicker pickerview will appear when you tap on the cityField.After that you need to add the following pickerview delegate methods that will fill pickerview with correct data. The data in my case is an array that holds a list of cities.</p>
  
  <pre><code>- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  {
      return 1;
  }
  
  - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  {
      return cities.count;
  }
  
  - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
  {
      return [cities objectAtIndex:row];
  }
  
  - (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
  {
      cityField.text = (NSString *)[cities objectAtIndex:row];
  }
  </code></pre>
  
  <p>If you want to have "Done" that will hide the UIPicker you'll need this code:</p>
  
  <pre><code>UIToolbar*  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];
  
      cityField.inputAccessoryView = mypickerToolbar;
  </code></pre>
  
  <p>And add a new method pickerDoneClicked</p>
  
  <pre><code>-(void)pickerDoneClicked
  {
      NSLog(@"Done Clicked");
      [cityField resignFirstResponder];
  }
  </code></pre>
  
  <p>I hope that this may help someone.</p>