IOS programming basics: How does the Hello World App work?

Source: Internet
Author: User

 

1. Because this is a technical article, some words use the original text to make the expression more accurate. 2. Due to the effectiveness of the level, translation may be inaccurate in some places. If there are any mistakes, please criticize and correct them.

I hope you have enjoyed the first iOS Programming Tutorial and created your first App. Before going to the next tutorial and making a more complex App, we need to go back and analyze this Hello World App. It is helpful for you to understand the syntax of some Objective-C language and the internal working mechanism of the App.

So far, you must have completed your first Hello World App according to the tutorial. However, after you complete this tutorial, you may have more questions in your mind:

  • What is the xib,. h,. m file used?
  • InshowMessageWhat is internal code? What is the purpose?
  • When you pressHello WorldWhat happened to the button? How does a button trigger the Message Display Action?
  • In XcodeRunHow does a button work?

     

    I hope you are familiar with the Xcode IDE development environment, so that I don't need to explain the above content again. For every developer, it is necessary for iOS programming to understand the internal details of the code and grasp the basic concepts. For some technical concepts, it is difficult to understand some technical concepts without any background in programming. But don't worry. This is just the beginning. If you continue to learn the subsequent tutorials and write more code, you will be able to better understand iOS programming. Do your best to learn more!

    Interface Builder, Header and Implementation Files

    First, what is the. xib,. h,. m file? This is a very good question raised by a reader. In the project navigation, you should find three main file types:. xib,. h,. m. (If you open the "Supporting Files" folder, you can find other file types, such as plist and framework. So far, we forget them and will discuss them in future courses .)

    . Xib
    • If a file also has a. xib extension, it is an Interface Builder file that stores the application UI. After you click the. xib file, Xcode automatically opens the Interface Builder Interface. You can drag and drop to edit the application UI. As shown in:

      Interface Builder in Xcode

      . H and. m
      • The file with the. h extension indicates that this isHeader file,. M extension indicates a specificImplementation. Like most other programming languages, the source code of Objective-C is also divided into two parts:InterfaceAndImplementation.

        To help you better understand these two relationships, let's take the TV remote control for example. We can easily adjust the TV volume using the wireless remote control. Press the volume + button to increase the speaker volume. When switching the channel, you only need to press the channel number. Let me ask you, do you know what happened when you press the volume button? You don't know. I believe most people do not know how the remote control communicates with the speaker. We only know that the button is used to adjust the volume. Here, the button isInterfaceAnd the details after the button are calledImplementation.

        Now you should have a deeper understanding of interfaces and implementations. Let's return to the Code. In Objective-C, the interface of a class is placed in.hFile. We use the syntax identifier@interfaceTo declare the interface of a class. Let's take a look at the specific implementation of HelloWorldViewController. h:

        @interface HelloWorldViewController : UIViewController-(IBAction)showMessage;@end

        The class name HelloWorldViewController starts with "@ interface. An internal implementation of "showMessage" is declared, which can also be called a method.

        Like the volume button, we obviously don't knowshowMessageHow this method works. You only know that it is used to display a piece of information on the screen. The specific implementation is put in the HelloWorldViewController. m file, as shown below:

        @implementation HelloWorldViewController// I've removed other methods for better reading. Focus on the showMessage method first.- (IBAction)showMessage {    UIAlertView *helloWorldAlert = [[UIAlertView alloc]                                initWithTitle:@My First App message:@Hello, World! delegate:nil cancelButtonTitle:@OK otherButtonTitles:nil];    // Display the Hello World Message    [helloWorldAlert show];}@end

        As shown above, you use "@ implementation" to declare an implementation. In "showMessage", the code is used to define a warning popped up on the screen. You do not need to understand the specific meaning of each line of code in the "showMessage" method. To put it simply, create a UIAlertView with "My First App" as the title and "Hello, World" as the message. Then, call the "show" method to request iOS to display a pop-up message on the screen. As shown in:

        Hello World App

        Surely you have understood the interface and implementation, right?

        Behind the Touch and Tap

        What happens when you press "Hello World? How does the "Hello World" button call the "showMessage" method to display the "Hello World" message?

        It reminds you of how the "Hello World" button is associated with the specific action of "sendMessage" in Interface Builder. Open "HelloWorldViewController. xib" again, select "Hello World", and click "Sent Events" in the Utility area to open the event.

        The send section shows all the links to events and actions. As shown in the preceding figure, the "Touch Up Inside" event is associated with the "showMessage" action. In iOS, apps are event-driven. Control/target listening for specific actions, such as touch and press. When an event is triggered, the target calls the preset action associated with the event.

        In our Hello World App, when a user raises his finger on the button, the "Touch Up Inside" event is triggered. Result, it will call the "showMessage" action to display the "Hello World" message.

        It intuitively shows the event stream described just now:

        Event and Message Flow of Hello World App

        Behind the Scene of the "Run" Button

        When you click "Run", Xcode loads the simulator and runs your App. But what happened after this scenario? As a programmer, you need to understand the entire process.

        The entire process can be divided into three parts: compilation, packaging, and running.

        Compile
        • You may think iOS can read Objective-C code. A big mistake. In fact, iOS can only read machine code. Objective-C code is easy for programmers to read and write code. We need to translate Objective-C source code into machine code so that iOS can understand the source code of your App. This process is called programming. Xcode already comes with a compiler for compiling source code. Package
          • Unlike other source codes, an App usually contains a large number of resource files, such as slice, text, and xlib files. All these resources must be packaged into the final App.

            We call the above two processes build.

            Run
            • Start the simulator and load your App. Long Luo created ~ 21: 06 May 5th, 2014 @ Shenzhen, China.
              Click Open Link

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.