Learning ios development from scratch (3): the first interactive app

Source: Internet
Author: User

Thanks for your attention and motivation, so that I can continue. It's really hard to get a good kid to bed at around 10 o'clock every day when my family has children and I live a long and old life, then let's get things of our own. It will soon arrive at, so we will go to bed. Now we are not in good health. If we don't go to bed early, we will definitely be out of work the next day, and we will depend on Red Bull, in this case, it is really difficult to squeeze out time to learn about ios. It is only a matter of time.

In the past few days, chapter 3 of the book mainly describes how to add a button and then add a response event to the button. When a button is clicked, the response event of the button is triggered, to change the text displayed on a Label. If this is put in VS, it is really a pile of dishes, but in ios, there is a big difference, and many things will be confused for the first time, the following describes how to create the first interactive app.

1) create a new project, select "Single View Application", name it "Button Fun", and save it.

Here, we will briefly explain what is "Automatic Reference Counting" (ARC for short), a garbage collection mechanism for ios, that is, to automatically release unused variables and release memory, this is a bit similar to garbage collection in C #, but it is not clear how they are implemented, remember that this is used to automatically release variables to free up memory space. For the moment, I think so. I will use it for more in-depth understanding and further details.

2) Add two buttons and one Label to select "BIDViewController. the method for adding a Button is the same as that for adding a Label in the previous article. You can drag the Button from the Object Library directly, find the "Round Rect Button" in the Object Library and drag two buttons to the interface. The effect after adding the Button is as follows, place the two buttons on the left and right sides of the Screen Based on the guides, and place the Label on the top of the screen. Then, extend the Label to the position where the guides appear.

3) Add the button text and remove the Label text in Xcode. There are three ways to change the text. The first method is to select the button, then, change the attribute value of the Title in Attributes Inspector. The second method is to double-click the button and type the required text. (This is quite different from, in VS, double-click a button to add a click event. In Xcode, the button text is changed, so VS is simple.) The third method is to use code to change the text.

Change the text of the Left and Right buttons to "Left" and "Right" respectively, and use the same method to remove the text on the Label, in addition, in Attributes Inspector, set the text alignment of the Label to the drama. The final interface effect is as follows:

4) Outlet and Action !) These two concepts are the first difficulty I have encountered since I learned ios development. In fact, they are difficult to understand.

Outlet:In short, it is the pointer in C/C ++, pointing to an object, or an object reference, such as a Label or a Button. In C # Winform, we will set a name for each control. During programming, you can directly use this name to operate the control, but in ios, the situation is somewhat complicated. The control does not have a name. That is to say, if you drag a control and the control does not have its own name, what should you do to operate the control in code? Define an Outlet and point the Outlet to the control. In code, use this Outlet to operate the control. Well, it's a little complicated.

Here are two examples of Outlet:

@property (weak, nonatomic) IBOutlet UILabel *statusText;

The above is just the definition of an Outlet, indicating that the Outlet will point to a Label control, but it does not indicate which Label to point.

@property (weak, nonatomic) IBOutlet UIButton *leftButton;

Similarly, the above is just an Outlet definition, indicating that the Outlet will point to a Button control, but it does not indicate which Button to point.

Action: Compared with Outlet, Action should be a little simpler. Its function is to define an event and then associate the control with the Action event. An example of Action is as follows:

- (IBAction)buttonPressed:(id)sender;

This is a bit similar to C # Winform. C # can define events separately, and then associate controls and events with the "+ =" operator. Although this syntax is not found in ios, however, you also need to contact. After the contact is established, the control can trigger this event.

I hope you can understand the Outlet and Action briefly. In fact, I have not fully understood it myself. I will write down my understanding first, I will try again later with a deeper understanding.

5) As mentioned before adding events to a Button, to add events, you must first create an Action and then establish a contact. Xcode is more intelligent and can help us with the two steps. A) Select BIDViewController. xib in project navigator to display the iphone interface. B) Select the second Show Assistant editor (shortcut: option + command + enter, menu bar View> Assistant Editor> Show Assistant editor) in the top-right corner of Xcode)

The selected interface is as follows. The BIDViewController is displayed on the right of the iphone interface. h file. We will drag the button to this file, and then make some corresponding settings. Xcode will automatically help us establish the connection between the control and Action. The next step is to create the Action. Select the Left button and press and hold the control key on the keyboard. Then move the mouse to the BIDViewController. h file on the right and open the mouse in the middle of @ interface and @ end.

(This image is captured from the book, so it is not clear) a box is displayed, as shown below:

Set the above value to the following content

First, we create an Action. Therefore, we select Action for Connection and name the event buttonPressed and UIButton as the type. This indicates a button event. Finally, click Connect to complete the creation. The code after creation is as follows: (BIDViewController. h)

#import <UIKit/UIKit.h>@interface BIDViewController : UIViewController- (IBAction)buttonPressed:(id)sender;@end

The method of this event is automatically added in BIDViewController. m.

- (IBAction)buttonPressed:(id)sender {}

After adding Action to the Left button, add the same event to the Right button. The parameter sender of buttonPressed can identify which button triggers the event, so we don't have to create an event for the Right button again. We can directly associate the Right button with buttonPressed. The association method is also very simple. You can select the Right button without placing it, press and hold the control key on the keyboard, and then move the mouse to the BIDViewController on the right. the upper part of the H file's buttonPressed event, and then open the mouse to complete the association. (This picture is captured from the book, so it is not clear)

6) the method for adding an Outlet to the Label is the same as that for adding an Action, but there is a slight difference in the parameter selection in the pop-up box. Similarly, select the Label with the mouse, and hold down the control, drag to BIDViewController. in the H file, after you open the mouse, a pop-up box pops up and the Connection retains the default value Outlet. Fill in "textStatus" in the Name as the Outlet Name, and keep the default value for all other options, click "Connect" to complete Outlet creation. The code after creation is as follows: (BIDViewController. h)

@property (weak, nonatomic) IBOutlet UILabel *textStatus;

In BIDViewController. m, @ synthesize is automatically added, and the release code for textStatus is added to the viewDidUnload method. (The viewDidUnload method is automatically created by the system. It is called when the View is released, that is, the View is not displayed. If other views are to be displayed, the method is called .)

@synthesize textStatus;......- (void)viewDidUnload{    [self setTextStatus:nil];    [super viewDidUnload];    // Release any retained subviews of the main view.    // e.g. self.myOutlet = nil;}

Now, all actions and Outlet have been added. The button can trigger the buttonPressed event, and the textStatus points to the Label to operate the Label.

7) It's really hard to write Code at last. Add the following code to the buttonPressed method of BIDViewController. m (in fact, there are only two lines)

- (IBAction)buttonPressed:(id)sender {    NSString *title = [sender titleForState:UIControlStateNormal];    textStatus.text = [NSString stringWithFormat:@"%@ button pressed.", title];}

The first line: titleForState indicates the title displayed when the button is in a certain state. UIControlStateNormal obtains the general state of the button, that is, the button is not clicked and the status is not obtained when the focus is not obtained, then, titleForState: UIControlStateNormal obtains the title of the button in the normal state. Row 2: Assign the title value to the Label for display. The two lines of code are easy to understand and have no difficulties.

8) Compile and run the shortcut key command + B to compile the program. Shortcut: command + R. Run the program. Of course, you can also click the Run button to compile and Run the program directly.

The following figure shows the effect of running the program.

Click the Left button. The Label displays "Left button pressed.". Click the Right button. The Label displays "Right button pressed ."

 

So far, the entire program has been developed!

9) thanks to the entire chapter, I found that there are many differences with Visual Studio, and there are great differences in events and control references, it may make faster progress in order to gain a thorough understanding and gain a deeper understanding. Of course, this is just the beginning. Next chapter: Learn More controls on the iphone!

 

Button_Fun.zip

 

P.S. this learning note is quite a long time away from the previous one. First, I am not familiar with iphone development and learn slowly. On the other hand, it is really time-consuming. Now, it is estimated that, writing a record in a week should be an ideal plan. I try my best to keep it for a long time and study hard. Thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.