Developing on iPhone (5): our first application (III)
Author: jack // Category: IPhone Application DevelopmentIn our previous article we had included in the interface of our application a Label, a TextField and a Button. Our aim was that the contents of the updated label was introduced with the TextField by clicking on the Button. We had (step 1) created the project, and (step 2) InterfaceBuilder used to define the screen. Continue with the remaining steps to leave the application running.
Step 3. Create variables in the ViewController.
We have our interface properly. In fact, if you did Build & Go might be leaving us see how:
Step 3. Create variables in the ViewController.
We have our interface properly. In fact, if you did Build & Go might be leaving us see how:
But obviously nothing happens in the press the button, because we have not done the minimum necessary developments. In this step 3 we will introduce variables and methods in the HelloWorldViewController, both in the interface (file. H) and implementation (file. M). To do this, as we indicated at the end of the previous post, we need to know that the TextField corresponds to a class UITextField, and the Label with a class UILabel. These 2 classes are within the framework UIKit.
If we have not closed and recorded Interface Builder, and what do we open the file in XCode HelloWorldViewController.h. The code now have something like this:
If we have not closed and recorded Interface Builder, and what do we open the file in XCode HelloWorldViewController.h. The code now have something like this:
We're already seeing some Objective-C. In this code we see an import declaration of the library UIKit, where classes UILabel and UITextField. We see how the interface is declared, and it extends from the generic UIViewController of UIKit.
Under the code of the interface include the following code:
We can now explain what we did:
We have declared within the block @ interface 2atributos type UILabel and UITextField, with 2 variable names and label * * textField respectively. IBOutlet appears ahead. What is this? We are told. IBOutlet is not a type of variable is a directive that Interface Builder helps you to know the existence of these variables UILabel and UITextField. Incidentally, remember that in the previous article you had taken a trick to know the types of variable objects of control (buttons, label, text field, etc.). In Interface Builder. On the other hand, the issue of * before the name of variable may be surprised to Java programmers, not so much to learn from C ... but it escribiréis Objective-C in the instance variables.
UILabel declare their properties and UITextField. nonatomic and retain what we will not for now, is related to memory management.
We see that we declare a method, UpdateText. He has a sign - in front, indicating that it is an instance method, and no class (for them is a sign). We can not come to explain to this because this is basic object-oriented programming ... This method does not return anything (ie, void) and receives a parameter of type id. It basically refers to the identifier of the object that will lead the call to our UpdateText method, which updates the value of the label. We can already guess that this will be the control button itself ...
Once done, we have finished with Step 3.
Step 4. Link controls the view with variables of the controller.
If we go on the one hand we have developed our interface with Interface Builder, and on the other we have those variables related to these controls in the HelloWorldViewController.h (interface of the class HelloWorldViewController.m controller). But still there is no connection between the light and the driver, that is, we have not given any instruction to relate, for example, the text field we built in Interface Builder with variable rate UITextField we've gotten into the class . Now we are going to perform this task. We opened again Interface Builder by clicking on HelloWorldViewController.xib, click on the box, and the Inspector palette, we are going to the second tab (Text Field Connections). We see that there is a section called Referencing Outlets, with no marked nothing. That means that at the moment Interface Builder is not aware of any connection between this text field and a IBOutlet (Interface Builder Outlet) of a class ... but we have a IBOutlet UITextField in our HelloWorldViewController, so let's link.
We have declared within the block @ interface 2atributos type UILabel and UITextField, with 2 variable names and label * * textField respectively. IBOutlet appears ahead. What is this? We are told. IBOutlet is not a type of variable is a directive that Interface Builder helps you to know the existence of these variables UILabel and UITextField. Incidentally, remember that in the previous article you had taken a trick to know the types of variable objects of control (buttons, label, text field, etc.). In Interface Builder. On the other hand, the issue of * before the name of variable may be surprised to Java programmers, not so much to learn from C ... but it escribiréis Objective-C in the instance variables.
UILabel declare their properties and UITextField. nonatomic and retain what we will not for now, is related to memory management.
We see that we declare a method, UpdateText. He has a sign - in front, indicating that it is an instance method, and no class (for them is a sign). We can not come to explain to this because this is basic object-oriented programming ... This method does not return anything (ie, void) and receives a parameter of type id. It basically refers to the identifier of the object that will lead the call to our UpdateText method, which updates the value of the label. We can already guess that this will be the control button itself ...
Once done, we have finished with Step 3.
Step 4. Link controls the view with variables of the controller.
If we go on the one hand we have developed our interface with Interface Builder, and on the other we have those variables related to these controls in the HelloWorldViewController.h (interface of the class HelloWorldViewController.m controller). But still there is no connection between the light and the driver, that is, we have not given any instruction to relate, for example, the text field we built in Interface Builder with variable rate UITextField we've gotten into the class . Now we are going to perform this task. We opened again Interface Builder by clicking on HelloWorldViewController.xib, click on the box, and the Inspector palette, we are going to the second tab (Text Field Connections). We see that there is a section called Referencing Outlets, with no marked nothing. That means that at the moment Interface Builder is not aware of any connection between this text field and a IBOutlet (Interface Builder Outlet) of a class ... but we have a IBOutlet UITextField in our HelloWorldViewController, so let's link.
To do this, as we see in the picture, clicking on the little circle next to "Referencing New Outlet", drag the File's Owner of the dialog window HelloWorldViewController.xib. We will no longer choose 2 options, view (the full view) and textField (our variable). Obviously we choose textField, and relate well with our pick from the variable of its kind UITextField. Indeed, the File'sOwner or owner of the file is none other than our class controller ...
We repeat the operation with the label, linking it with our variable controller's label. And with the button, is something different. In this case we are going to connect with our operation UpdateText, but for a specific event. Do this with the event 'Touch Up Inside', and choose our method UpdateText:
We repeat the operation with the label, linking it with our variable controller's label. And with the button, is something different. In this case we are going to connect with our operation UpdateText, but for a specific event. Do this with the event 'Touch Up Inside', and choose our method UpdateText:
And in this way because we do have linked the controls that we had identified with Interface Builder and the variables that we have gotten into the driver. We close Interface Builder recording everything and we are going to step back to XCode.
Step 5. Recent developments.
As we open the XCode HelloWorldViewController.my put the following code to us that our class is as follows:
As we open the XCode HelloWorldViewController.my put the following code to us that our class is as follows:
If you look good, we just got the block @ synthesizer, and the implementation of the method declared in the interface. H, UpdateText. Under this code will have much commented code.
@ Synthesizer line does something similar to generate the typical getter / setter (accesores / mutadores) typical in the language OO (encapsulation). The need for the driver you get the content, for example, the text box.
The method UpdateText, as you see, simply updating the text of our label with the contents of the text field. We've finished.
Now, we simply Build & Go, and you will see that by putting a string of text and click on the button, the contents of the label is updated. In the following article will introduce enhancements to our application.
@ Synthesizer line does something similar to generate the typical getter / setter (accesores / mutadores) typical in the language OO (encapsulation). The need for the driver you get the content, for example, the text box.
The method UpdateText, as you see, simply updating the text of our label with the contents of the text field. We've finished.
Now, we simply Build & Go, and you will see that by putting a string of text and click on the button, the contents of the label is updated. In the following article will introduce enhancements to our application.
0 Responses to "Developing on iPhone (5): our first application (III)"
Post a Comment