UIAlertView Text Fields

Shaun, Jul 6

As I'm sure you already know, UIAlertView supports text fields. It has since the OS 1.0 toolchain, and for whatever reason, Apple has them as private methods. Even when adding a category to support the private methods, things are a little quirky. You'll notice that the keyboard is pretty much black on 3.0, and on 2.0, you actually see multiple keyboards being deployed. That's also the reason it's black, there's two translucent keyboards, giving you a black keyboard.

I've struggled with this on and off through multiple apps for over a year now, but I finally have this working smoothly.

So here's how you do it:

1. Create a category for UIAlertView to enable UITextFields:

We override the - (id)keyboard method, so it doesn't deploy it's own keyboard, which screws things up.

2. Create your UIAlertView and add the text fields

We send becomeFirstResponder on a delay, so the alertView has a chance to start appearing, if we send it before it starts appearing, the animation is choppy or it doesn't animate at all and just appears.

3. Clean up in the delegate methods

This is pretty important, if you don't do it yourself, the keyboard gets stuck on the screen until after the alert has disappeared, which isn't ideal.

Thats it!

Keep in mind, these methods are private, which means they're subject to change. They haven't changed since iPhone OS 1.0 though.

blog comments powered by Disqus