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:Interface
AndImplementation
.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 isInterface
And 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.h
File. We use the syntax identifier@interface
To 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 knowshowMessage
How 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 TapWhat 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" ButtonWhen 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