Wednesday, September 28, 2011

Do you know Objective-C? UITextField



Today we are going to learn how to use UITextField and how we have to do if we want to share data between two or more views.
To begin with, UITextField is no more than a place where we can write anything by using keyboard, to use any action with it.  If we want to use a UITextField, we have to know we need a method to display the keyboard to setting UITextField. Also, the UITextFieldDelegate protocol defines the messages sent to a text field delegate as part of the sequence of editing its text. This is why we set the UIView as delegate of the UITextField. We get it by creating the following method:


We use two UITextField with our UILabel which will show UITextField's contents. For this example we use a button to hide the keyboard, but it can be designed to have to whatever behavior its.


The method resignFirstResponder makes the UITextField loses focus and hides keyboard. After actions and objects have been designed we can set the NIB file.


We design a navigation bar with a button which makes to lose focus to the selected UITextField and then, textFieldDidEndEditing will be called to set the UILabel. Also we link the keyboard method when UITextField Did End On Exit like is showing in the past image.


By last, if we want to share data between two UIViews, we have to instantiate a objet of AppDelegate like this:  (iAccesoAppDelegate *)[[UIApplication sharedApplicationdelegate]. Also we have to set the shared data with @property as readwrite and copy. With this, we are setting the UIApplication as delegate of our View where we can have shared data, and both could access to them.

No comments: