As lechec points out just set the UITextField's inputView property to a UIPickerView. It is very easy to do.
This blog post goes into detail on how to do it: http://nomtek.com/tips-for-developers/working-with-pickers/ and there is a project included for download with sample code. The project opens fine in XCode 4.2 and compiles for iOS 5.
Additionally, I didn't want to put in a button for hiding it. The following code allows the user to tap anywhere in the View, outside of the UITextField and resign it as first responder, basically making it 'lose focus'. Actually the code to handle the tap gesture causes any UITextField to 'lose focus', thus hiding any inputView or keyboard.
-(void)handleViewTapGesture:(UITapGestureRecognizer *)gesture
{
[self endEditing:YES];
}
This is implemented in the ViewController. The handler is added as a gesture recognizer to the appropriate View in the View property's setter:
-(void) setLoginView:(LoginView *)loginView
{
_loginView = loginView;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.loginView action:@selector(handleTapGesture:)];
[tapRecognizer setDelegate:self]; // self implements the UIGestureRecognizerDelegate protocol
[self.loginView addGestureRecognizer:tapRecognizer];
}
The handler could be defined in the View as well. If you are unfamiliar with handling gestures, see Apple's docs and/or tons of samples elsewhere.
I should mention that you will need some additional code to make sure other controls get taps, you need a delegate that implements the UIGestureRecognizerDelegate protocol and this method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UIButton class]]) // Customize appropriately.
return NO; // Don't let the custom gestureRecognizer handle the touch
return YES;
}