Skip to content

Greetings: Enhance Code


Make Keyboard Disappear

The Keyboard is not going away when we press any button. Because that covers nearly half of the screen we want to make sure that it goes away after we don’t need it anymore.

On our View

The easy part is to make the keyboard go away is when we press one of our buttons. This can be easily accomplished by telling the Text Field to give up it’s role as First Responder meaning being the object that receives the input from the keyboard. This is accomplished by adding this line:

1
[greetInput resignFirstResponder];

in both methods: greetMe() and clearAll().

Done Button

The Keyboard is out of our control and so we don’t have an apparent way to handle the event when the Done button is clicked. The keyboard is provided by the Text Field and so let us have a look at the documentation for the UITextField. In case you don’t know how to get there please have a look at the XCode Development Environment>>XCode Development Environment[doesn’t exist] page. When searching for the UITextField you will find this:

Attachment “xcode.documentation.text.field.delegate.png” not found

Now we know we need to implement the method textFieldShouldReturn() provided by the interface UITextFieldDelegate. In order case we have to put our code inside the iGreetingsViewController. First we need to make sure that the header implements the interface which is done by adding at the end of the interface definition between < and >: \

1
@interface iGreetingsViewController : UIViewController <UITextFieldDelegate> {

Now we need to implement that in the class with:

1
2
3
4
- (BOOL) textFieldShouldReturn: (UITextField *) textField { 
        [textField resignFirstResponder]; 
        return YES; 
}

This means that the event caused by pressing the Done button will call our method and we can release the input field as first responder. Still when we build and execute the application nothing is happend, why?. \
\

So let us think that through. We have the UI and we implemented the code and so why is it not working. Well, last time when we coded the methods we had to wire the up with the UI. So let us try to see the context menu on the input field (Control or right-click) and you will see on top a category called Outlets with a single entry called delegate. Now let’s try and wire that up with the File’s Owner with is in that case our iGreetingsViewController:

Attachment “iGreetings.input.field.delegate.png” not found

after that the context menu of the Input Field should look like this:

Attachment “iGreetings.input.field.context.png” not found

Finally we can again build and execute the application and now while clicking on the Done button the keyboard goes away. Still there is some missing because the greetings will not be display and that is because we did not execute the greetMe() method. So let’s do that:

1
2
3
4
- (BOOL) textFieldShouldReturn: (UITextField *) textField { 
        [self greetMe:nil];
        return YES; 
}

\ \

  • 2nd line: call the method greetMe() with an nil parameter (is like Java’s null) which will create the greetings and release the first responder status of the input button. If you like you could also clear the input field at the end. Launching the application now would yield in a result like this:

Attachment “iGreetings.final.coding.launch.png” not found

Conclusion

In that page we learned that hooking into the iPhone framework we have to deal with delegates. These delegates are what the framework calls in order to let the application know what is going on in the background and giving the application a chance to control what is going on. For example when we return FALSE in the textFieldShouldReturn() method we could prevent the UI from executing the default handling of the Done button.

Later then we learned that we have always to wire up the UI with the backend to make sure we receive the delegate’s calls because the framework will not blindly call any delegate’s methods.