views:

752

answers:

1

Is it possible for a UIPickerView to appear instead of a keyboard when a UITextField is selected? Every option in Interface Builder is some sort of keyboard.

I suppose I could create a UIPickerView programmatically and create one when the UITextField registers a touchUpInside event, then tell the UITextField to resignFirstResponder, but that seems a bit of a hack.

Is there an "official" or more "correct" way to do this?

Thanks!

+3  A: 

You can implement this code and override the default behaviour (that is, showing the keyboard):

#pragma mark -
#pragma mark UITextFieldDelegate methods

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil 
                                                    message:@"Bouh!"
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    return NO;
}

Instead of showing a UIAlertView, you might as well show your own UIPickerView and do your thing.

Adrian Kosmaczewski
@akosma fantastic idea! I didn't realize that this would override showing the keyboard. If you were to call [super textFieldShouldBeginEditing:textField] that would show the keyboard, right?
John Frankes
Not necessarily; textFieldShouldBeginEditing: is a delegate method, and is not part of any hierarchy. You should "return YES" in order to show the keyboard. In your logic, then, you should decide whether or not to show the keyboard (returning YES or NO does the trick ;)
Adrian Kosmaczewski
Thank you akosma, I found this purely by chance while taking a break from painfully trying to write a replacement keyboard. Procrastination FTW!
willc2