Learn iOS development from scratch (iii): first interactive app

Source: Internet
Author: User

Thank you for your attention, but also give me a motivation, let me continue to move forward. Have their own family have children, live on there is a small life, can squeeze a little time to learn really difficult, every day to fix the child to sleep is about 10 o'clock in the evening, and then make their own things, a blink of an eye soon to 12 points, will go to sleep, now the body soup is not strong ah, if not to sleep early, The next day to work is certainly not spirit, to rely on Red Bull, hehe, in such circumstances to squeeze out time to learn iOS really difficult, can only be jianfengchazhen, seize all available time to learn, time, squeeze a squeeze is always there, just how many problems.

These days it seems that the third chapter of the book, mainly on how to add a button, and then add a response to the button event, when the button is clicked, trigger the response of the button event, thereby changing the display text on a label. If this is in VS, it's really a piece of cake, but in iOS it's very different and many things are confusing the first touch, and here's a little bit about how to create the first interactive app.

1) Create a new project, select "Single View Application", name "button Fun" and save.

Here's a simple explanation of what "Automatic Reference counting", or arc, is an iOS garbage collection mechanism that automatically frees unused variables and frees up memory, which is a bit like garbage collection in C #, but as to how they are implemented, is not very clear, anyway remember this is used to automatically release variables to free up memory space on it, for the time being so think, later with more in-depth understanding, and then detailed description.

2) Add 2 buttons and a label select "Bidviewcontroller.xib" in Project navigate, add the button to the same method as in the previous article, directly from the object Library inside drag can, found in the object Library "Round Rect button" Drag 2 to the interface, after the effect is added below, the 2 buttons respectively according to the auxiliary line on the left and right side of the screen in the upper and lower play position, Label on top, then lengthen the label and pull it to the position where the guides appear.

3) Add button text, remove the label text in Xcode, there are 3 ways to change the text, the first to select the button, and then change the property value of the title in Attributes Inspector the second method is to directly double-click on the button with the mouse, Then type the text you want (this is a lot different from VS, in VS, double-clicking a button is adding a click event, and in Xcode, it's the text that changes the button, so it's easier to say vs). The third is to change the text in code.

respectively, the text of the left and right two buttons is changed to "leave" and "right", using the same method, the text on the label is removed, and the text alignment of the label is set to play in Attributes inspector, the final interface effect is as follows

4) Outlet and action (this section of the narrative is not necessarily entirely correct, but my personal understanding, if there is not the place, hope you can point out, thank you! These two concepts are my first difficulty in learning iOS development, in fact, it is difficult to say, is very engaged, at first did not understand its meaning.

Outlet: in simple terms, a pointer to an object, or a reference to an object, such as a label, a button, and so on. In C # WinForm, we will set a name for each control, so you can manipulate the control directly using this name when programming, but in iOS it gets a little complicated, the control doesn't have a name, and you drag a control over it, This control does not have its own name, so what should I do to manipulate the control in code? Define a outlet, then point this outlet to the control, and use this outlet in code to manipulate the control, which is a little bit more complicated.

Examples of two outlet:

@property (Weak, nonatomic) Iboutlet UILabel *statustext;

The above is just a outlet definition, stating that this outlet will point to a label control, but it does not indicate which label to point to.

@property (Weak, nonatomic) Iboutlet UIButton *leftbutton;

Again, the above is just a outlet definition, stating that this outlet will point to a button control, but it does not indicate which button to point to.

Action: it should be a little simpler relative to Outlet,action, which is to define an event and then connect the control to the action event. An example of action is as follows:

-(Ibaction) buttonpressed: (ID) sender;

This is a bit similar to C # WinForm, where C # can define events individually, and then connect controls and events using the "+ =" operator, although iOS doesn't have this syntax, but it also needs to be contacted, and the control can trigger the event once the connection is established.

Hope that the above on the outlet and action of the simple said that we can understand, in fact, I do not know the hundred understand, the first to write their own understanding, after a more in-depth understanding and then to supplement.

5) Adding an event to the Button said before, to add an event, first to create an action, and then establish a contact, Xcode smarter, you can help us to take these 2 steps together. A) Select Bidviewcontroller.xib in Project Navigator to display the iphone's interface. b) Select the second Show Assistant editor (shortcut: Option+command+enter, menu bar view>assistant editor>show) from left to right in the top right corner of Xcode (7 buttons) Assistant editor)

After the screen is selected, the BIDViewController.h file will appear on the right side of the iphone interface, we will then drag the button to the file, and then make some corresponding settings, Xcode will automatically help us to establish a link between the control and action. The next step is to create the action, click on the left button, press and hold the control key on the keyboard, then move the mouse to the BIDViewController.h file on the right, release the mouse in the middle of @interface and @end

(This picture is intercepted from the book, so it's not clear) there will be a box bouncing out, as follows

Set the above value to the following content

First, we create an action, so connection select action, then name the event buttonpressed, type UIButton, the button event, and finally click the Connect button to complete the creation. After the creation, the following code is completed: (BIDViewController.h)

#import <UIKit/UIKit.h>@interface  bidviewcontroller:uiviewcontroller-(ibaction) Buttonpressed: (ID) sender; @end

The method that will automatically add the event in BIDVIEWCONTROLLER.M

-(Ibaction) buttonpressed: (ID) sender {}

After adding an action for the left button and then adding the same event to the right button, the buttonpressed parameter sender can tell which button triggered the event, so we no longer have to create an event for a separate Directly associate right button to buttonpressed, the associated method is simple, the mouse is selected right button, press and hold the control key on the keyboard, Then move the mouse over the Buttonpressed event on the right side of the BIDViewController.h file, then release the mouse and the association is complete. (This picture is intercepted from the book, so it's not clear)

6) Add outlet add outlet to the label method and add the action method, just slightly different in the pop-up box parameter selection, similarly, the mouse to select the label label, hold Control, Drag into the BIDViewController.h file, after releasing the mouse, a pop-up box pops out connection leave the default value Outlet,name fill in the "Textstatus" as the name of the outlet, the other options are left to the default value, and finally click "Connect" button, outlet creation is complete. After the creation, the following code is completed: (BIDViewController.h)

@property (Weak, nonatomic) Iboutlet UILabel *textstatus;

The @synthesize is added automatically in BIDVIEWCONTROLLER.M and the release code for Textstatus is added to the Viewdidunload method. (The Viewdidunload method is automatically created by the system and is called when the view is released, that is, the view will not be displayed, and the method will be called when the other view is to be displayed.) )

@synthesize Textstatus; -(void) viewdidunload{    [self settextstatus:nil];    [Super Viewdidunload];     // Release Any retained subviews of the main view.     // e.g. Self.myoutlet = nil;}

Now that all the actions and outlet have been added, the button can trigger the Buttonpressed event, Textstatus points to the label and can manipulate the label.

7) write code at last to start writing, really not easy ah. Add the following code to the BIDVIEWCONTROLLER.M buttonpressed method (actually 2 lines)

-(Ibaction) buttonpressed: (ID) sender {    *title = [Sender titleforstate: UIControlStateNormal];     = [NSString stringWithFormat:@ "%@ button pressed. " , title];}

The first line: Titleforstate refers to the button in a state when the title,uicontrolstatenormal to get the general state of the button, that is, the button is not clicked, the state when the focus is not obtained, then titleforstate: UIControlStateNormal gets the title of the button in its usual state. Second line: assigns the title to the label for display. The two lines of code are simple and easy to understand, without any difficulty.

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

The starting effect after the program runs is as follows

Click the left button and the label displays "left button pressed.", click the right button, and the label displays "right button pressed."

At this point, the entire program development completed!

9) Thanks to the entire chapter to learn, found that there are many and Visual Studio different places, events, control references and so there are relatively large differences, there is a comparative study, may make faster progress, to learn thoroughly, understand more deeply. Of course, this is just the beginning, next chapter, to learn more about the controls on the iphone, the next chapter goodbye!

Button_fun.zip

P.S. This study note distance from the previous article spaced a long time, one is unfamiliar with the development of the iphone, learning slower, on the other hand is really limited time, now estimated that a week to write a record should be a more ideal plan, I try to strive not to delay too long, hard study, Thank you for your welcome!

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.