When I'm starting to learn a new language or framework what gets me pumped up is to see some UI eye candy, real out-of-the-box stuff. And as you might guess with the iPhone there is a lot of this with the UI controls. My first steps were to drag a button and a textbox from the interface builder and see a message box popped-up with the TexBox's text when the button is pressed.
Code. Build. Run. Wide neophyte eyes on the awesome iPhone emulator - Apples really done a good job here. I clicked the textbox control I just made - sweet keyboard and bang! I cannot escape the keyboard. I clicked the return button like a thousand times and got myself thinking.. wow.. I just got started and there's a major Bug.
.. or was it? As it turns out ..
What you have to do is implement the return button functionality. Which after some thinking it makes sense - this is the first real paradigm shift away from languages like .Net. First you must set the text input traits attribute: Return Key to Done on the text field in the Inspector (Attributes Tab). When the user taps the Done button, a "did end on exist" event will be generated. At that time you need to tell the text field to give up control so that the keyboard will go away. In order to give up control, you need to add and action method to your controller class.
- (IBAction) textFieldDoneEditing:(id)
When the emulator appears click the text field, type something, and then tap the Done button and the keyboard disappears. Alright! But what about the numeric keypad? Where is the Done button on that one? Well, crud! Not all keyboard layouts feature a Done button. So what's the solution for that numeric keypads?
The answer is probably going to surprise you because of its simplicity. You need to create an invisible button that sits behind everything else and does nothing except tell our text fields to yield first responder status when it detects a touch. It's perfectly alright to call resignFirstResponder on a control that is not the first responder. So you can call it on all your view's text fields without having to check which one is the first Responder(if any).
I know this is a but awkward but.. it's that's the way it is. There are probably some other ways to do it (which you are welcome to comment about them) but this how I've seen it on a couple of forums and some books.
My next post will be about some slick animations done on iPhone when you change the view.
Side note: Add the Date Picker Control - even if you don't do anything with it at first - it's just so damn cool.