iOS Fury Road---what you can learn in the first app of iOS

Source: Internet
Author: User
Tags print format print object

First, the previous article review
  
Prior to the introduction of the iOS Learning Roadmap, because there are some Android development problems, so the delay for some time, then the next time we will continue to start the fury of iOS learning, according to international practice, the first application of course is our HelloWorld program. Then this article will be a simple program to explain the program life cycle in iOS, the application of a few key objects, project structure, and finally in hand to create an empty project.
  
Second, the establishment of simple procedures HelloWorld
  
The following first use Xcode to create a new HelloWorld program:
  
Click Next to:
  
This is very similar to our new Android program in Androidstudio, not much, just click Next:
  
So the project is set up, and below we let the program run up and show a HelloWorld text. This is done in two ways, similar in Android, where we use Uilabel to operate. Similar to the TextView control in Android, one way is the code-writing layout, which is a drag-like layout file in Android. In order to facilitate and later will be detailed introduction of the way to drag, directly in the code to write.
  
So where do we write the code? The entry here is in the root controller Viewcontroller class:
  
This portal is similar to the OnCreate method of activity in Android, we usually initialize a view here, see the code logic is not very complex, but there are many hidden knowledge points, but not the focus of this article, such as this involves the coordinates of iOS, The relationship between the view and so on, this is in the later chapters will detail the view knowledge points in the analysis. After the code is finished, run the program and it's easy to run:
  
Click the Run button in the upper-left corner and select the target device for the project to run. The results of the operation are as follows:
  
As you can see here, a simple HelloWorld is displayed in a 4s device. Well, here's what we're going to start with today, what we can learn from this simple program.
  
Third, the process of knowledge points analysis
  
First, the entry and life cycle of the procedure
  
When we developed the Android program, we all know that the entrance of an application is not mainactivity OnCreate method nor application OnCreate method, but the main method of Activitythread, Similarly, the entry for a program in iOS is also the main method, and this main class is in the Main method:
  
Each program has a main.m this class, there is a main method inside, and this method we see and C language in the form of the main function is consistent, the entrance is here, then one of the things here is to entrust the application of the proxy object Appdelegate class, that is, the entire application of the logic entrusted to the app The delegate class, which is called a proxy in iOS, is usually called a callback mechanism in Android, and then the Uiapplicationmain class interacts with the Appdelegate class, such as the life cycle of the application, event handling, etc. So here's a look at the Appdelegate class:
  
In this class we can see a lot of timing callbacks, which are associated with the application's life cycle approach, and here we analyze the callback timing of these methods:
  
1. Tell the agent to start the basic Completion program ready to start running
  
-(BOOL) Application: (UIApplication *) application www.yghrcp88.cn didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions
  
2, tell the agent process to start but not yet into the state to save
  
-(BOOL) Application: (UIApplication *) application willfinishlaunchingwithoptions: (nsdictionary *) launchOptions
  
3. When the application is going to be inactive, during which time the application does not receive messages or events, such as coming to the phone
  
-(void) applicationWillResignActive:www.huacairen88.cn (uiapplication *) application
  
4. Called when the program is pushed to the background. So to set the background to continue running, you can set it in this function
  
-(void) Applicationdidenterbackground: (uiapplication *) application
  
5, when the program from the background will be back to the front of the call, this is just the opposite of the above method
  
-(void) Applicationwillenterforeground: (uiapplication *) application
  
6. When the application executes in the active state, this is just the opposite of the method above
  
-(void) Applicationdidbecomeactive: (uiapplication *) application
  
7, when the program is going to exit is called, is usually used to save data and some cleanup work before exiting
  
-(void) applicationWillTerminate:www.zhenloyl88.cn (uiapplication *) application
  
8. When the application receives a memory warning callback method
  
-(void) applicationdidreceivememorywarning:www.yinbaovip.cn/(uiapplication *) application
  
After reading these life cycle callback methods, you might think that this is very similar to the activity in Android, not the application class, which is more complex in Android, typically has three physical key relationships activity life cycle, There is only one home key in iOS, so the life cycle here is well understood.
  
Second, the application of the window
  
The above describes the iOS program life cycle related class Appdelegate, the following also need to continue analysis, how to correlate the Viewcontroller class, the HelloWorld show out?
  
First, we print a log in the callback method after the program starts, printing the current app with the controller:
  
Note: The print log method in iOS is similar to Android, using string formatting to print, the placeholder for a generic print object is%@, and if you want to print an object-specific value, you need to implement the description method of the class. This is the same as the ToString method in Java. Here are a few more words, that is, the format of the general printing basic type is as follows:
  
%d,%i integral type (%i's old notation)
  
%HD Short-integer
  
%ld,%lld Long Integral type
  
%u no-character integer
  
%f floating-point and double type
  
%0.2f Precision floating-point number, reserved only two decimal places
  
%x is a 32-bit unsigned integer number (www.zhenlyule.cn/unsigned int), printed using the number 0-9 hexadecimal, lowercase a-f;
  
%x is a 32-bit unsigned integer number (unsigned int), printed using the number 0-9 hexadecimal, uppercase a-f;
  
%o Octal
  
%p Pointer Address
  
A string of type%s char*
  
%c Char Character type
  
%c Unichar Type
  
And these types of print format without the back, with more than the nature to remember, and the most used here should be%i,%s,%p,%c,%f these several.
  
The printing results are as follows:
  
See what lifecycle callback methods will execute when an app starts, and see that the root controller is the entry Viewcontroller class object on which we added the HelloWorld control. So here are two objects: one is the selfwww.myqunliphoto.com. Window object, and the other is the Window.rootviewcontroller object. Let's take a look at the object of Self.window, in fact, this object is a UIWindow type. As with Android, a program that corresponds to a window is the basis for subsequent display of the view.
  
Usually an application corresponds to a UIWindow, is the application of the window, but this can not be said too absolute, because the back of either Android or iOS will appear in the multi-screen development mode, there will be more than one window. He is the cornerstone of the follow-up display view, if there is no such uiwindow, then the application of all the UI will not show, in fact, UIWindow is a special view, he inherited the UIView class, You can also add a view control directly and you can manipulate it as you would with view:
  
And the above is said, an application can have only one uiwindow, but we can define more than one UIWindow, and then set the specified UIWindow as the main window, is also possible. If you want to make the window a main window and show it, just call the Makekeyandvisible method.
  
Third, the application of the root controller
  
The above describes the UIWindow knowledge points, the following look at the Uiviewcontroller class, in iOS Uiviewcontroller class is equivalent to the activity in Android, to control the display function of the view, He is a carrier of every page in an app, and in Android a program has a default or main activity, and in iOS, there is a root controller, which is the only window that needs to be set to the app. That is, the Rootviewcontroller attribute value of UIWindow. There are some life cycle methods for the controller, and the most important one is the method after the view load is completed:
  
-(void) viewdidload
  
This method is somewhat similar to the OnCreate method of activity, and we do it in this way if we want to add the view ourselves. From the example above we can see that in this method we define a Uilabel property, and each controller has a very important attribute is the parent view, we need to add the view can be added by the Addsubview method.
  
Attention:
  
Adding a view to iOS is simple and convenient because any one of the controls, including the UIWindow mentioned above, inherits the UIView class, and these controls can be arbitrarily added to other controls without any restrictions. For example, you can add a label to a button UIButton Uilabel completely OK, but if you are not on Android, Android if a control to add sub-view, he must inherit ViewGroup, for example, we often use several layout classes, This looks like iOS is really much simpler than Android.
  
We can see that an application will definitely contain more than one controller, because the general application will contain multiple pages, and each page can also jump between the activity in the Android jump using intent to operate, but there is no such mechanism in iOS, Instead, a particular controller is used to manage the class, usually using the navigation controller and the tab controller to do the management, the specific knowledge to the back to accept the controller in detail explained.
  
Iv. summarizing four object relations
  
Here, we're done analyzing a very important four objects in an iOS program: Uiapplicationdelegate,uiwindow,uiviewcontroller,uiview
  
From this diagram, you can see that an application must contain the proxy object Uiappdelegate class applied by the program, and the application window UIWindow class. The window also has a root Controller page Uiviewcontroller class, each controller has a parent view of the presentation UI, and the root controller is no exception. Finally, each controller is associated with a parent view UI that is used to present each page.
  
Iv. Creating a new Empty project
  
Once we know the structure of an application and the key objects, let's start by creating a new empty project and then creating the four key objects ourselves. Because after the Xcode8 is not supported to create a new empty project, in order to create a new empty project, we can create a new project and then delete the class, it becomes this:
  
First step: Create a new Uiviewcontroller root controller
  
Be sure to select the Cocoatouch class file:
  
Here we are in fact the equivalent of the completion of a simple application, the following need to set the callback method in the application window information and the root controller information can be:
  
At this point the running program can see a red application:
  
From here can be seen, an application in fact the most basic elements must have uiapplicationdelegate and UIWindow and root controller can, then this is actually a simple application, but in the actual development is certainly contains a number of controllers, And each root controller corresponds to a view layout file, so the following will start to create the corresponding view information. Of course, here can be in the controller's callback method to manually code to add view, but here we are in the introduction of iOS and Android, each controller has a corresponding layout file, which is called Xib file.
  
Step Two: Create a new Xib file
  
Choose Next, Name Root.xib to
  
After the new is complete, you can see nothing:
  
We need to create one step at a.
  
Step three: Correlate Xib and controllers
  
Then we set the current Xib file ' s owner to our root controller, which must be set Xib file ' owner, or it will be an error.
  
So from here we can see that the xib is actually equivalent to the layout file XML in Android, each layout file is associated with an activity, and here is the same principle, a xib must rely on a controller, so here the file ' s The owner can set the corresponding controller.
  
Attention:
  
But the Xib file in iOS and the layout file in Android is a little different, here xib not only can set the layout of the view, and then introduce a case, here can contain some special objects, the following describes how to put the Controller, window and other objects into a xib, There's no need to write code to create an app, and what's the difference between Xib and Xcode created by default Stroyboard? It is easy to understand that Stroyboard better manage the relationship between multiple controllers and views, such as now I want to create a controller, then create the corresponding Xib file, and if there is more than one controller page in a project, then there will be multiple Xib files in the project, which may not be good management , and Stroyborad can put these files together to manage, but from other iOS veteran know, this actually still want specific project and everyone's development habit, not strongly request must use Stroyborad.
  
Fourth Step: Add the parent view in Xib
  
The above set up the relationship between the Xib file and controller, the following also set the controller's parent view property value, first we have to add a view to this xib, if not set, run the program will error, add view is simple, We have an interface in the bottom right corner of Xcode that allows you to select various controls and objects:
  
Search the view control, and then drag it directly to the xib content. After the success, we also need to link the controller's view property to the view control, otherwise it will be an error. This is very simple, for Xcode development tools, as long as a drag connection. As shown in the following:
  
Attention:
  
There is a handy feature in iOS, which is to assign values to objects and add events to the control, sometimes you can choose to drag this shortcut, but there is a premise that this property must be set to the Iboutlet attribute, set to see a small circle before the property:
  
Indicates that it will appear in the dragged list, and then if you want to assign it to this property, drag it to the specified object, so you can see the difference between the Xib in iOS and the layout file in Android. There is also the Ibaction feature, which is mainly the event of adding a control, this is usually added in a method, then this method will appear in the list of drags, for example, we define a method in the controller:
  
Here you can see there is also a small circle, the following in return to see Xib in the file ' s Owner:
  
As you can see here, when you go to drag the DoClick method to the right of the plus circle to a UIButton control, the interface appears, which lists multiple events, we can select one after that, it means that the UIButton control of this event is triggered by the DoClick method. From this can be seen to assign values to the view control and add the event method is very convenient, and in Android need to Findviewbyid method, and then set some event callback, of course, there are now some open-source framework in Android to do this similar function, Mainly uses the annotation function, unordered Findviewbyid assigns the value and adds the event. But I do not like to use this framework, feel native best!
  
Fifth step: Loading the Xib file
  
OK, here we have a new root controller and the corresponding xib file, then the following if you want this controller to show this xib content, still need to do some work is to manually load the Xib file, the above connection operation is simply to establish a relationship, loading also have to write their own code, is equivalent to the Setcontentview method effect in Android, here you need to change the controller initialization method:
  
The controller provides a initwithnibname:bundle: method, this method can display the loading of a xib file, it is important to note that two points is Xib file name can not carry the suffix name, followed by the second parameter represents the path of the Xib file, Passing in nil means looking in the root directory of the project.
  
After you have set up the controller to load the Xib code, you can run the program below:
  
After seeing the effect, we found that not only can we manually write code in the controller to add view, but also we can use the Xib file for page layout.
  
Here we have a new simple project, in this process we will learn more about the program of four important objects of the relationship, the following to summarize:
  
1, Uiappdelegate class
  
This class is primarily used to manage the lifecycle of applications and callback methods for some events, and each application will have one of these classes.
  
2, UIWindow class
  
This class is primarily used to display the window of the UI of the application, which is actually a special uiview that can manipulate it like a control, and each application has a unique main window, the Window property value in the Uiappdelegate class.
  
3, Uiviewcontroller class
  
This class is equivalent to the function class that manages each page in the app, he is very similar to the activity in Android, and an application typically contains multiple controllers, but there must be a root controller, This controller is typically set to the Rootviewcontroller attribute value of UIWindow. At the same time each controller if you want to use code layout, in the Viewdidloaded method to add, if you want to use the Xib layout file to operate, you need to manually load the operation, using the controller's Initwithnibname:bundle: method.
  
4, UIView class
  
In fact, this class is the parent of each control in iOS, but each controller has a parent view, which is the UIView class, which is typically set to the Uiviewcontroller view property value.
  
V. Knowledge Point Supplement
  
First, parse the Xib file
  
Parsing process for xib files:
  
1> Create a good objects all objects below
  
2> establish a relationship between all objects under Objects and file's owner
  
As mentioned in Xib above, xib can add not only control views, but also various objects, such as here we can add a UIWindow and Uiviewcontroller object:
  
See, a xib can actually manage all the objects of a simple application, and then we can have this relationship with these objects just fine.
  
Second, apply four objects to set up relationships and steps
  
1> Call the Init method or Initwithnibname:bundle: method to create the controller
  
Self.viewcontroller = [[Rootviewcontroller alloc] init];(The controller is created and does not initialize its internal view immediately)
  
2> setting the controller to UIWindow root controller
  
Self.window.rootViewController = Self.viewcontroller; (The controller's view is not added to the window after the root controller has been set)
  
3> Displays the window and adds the view of the Controller to the window
  
[Self.window makekeyandvisible];
  
First call the controller's Loadview to initialize the controller's view (can be created via the Xib file \ Code)
  
Third, the invocation order of the Uiviewcontroller life cycle method
  
When 1> view is initialized, the controller's Viewdidload method is called
  
When the 2> view is initialized, the view of the root controller is added to the window
  
3> The controller's viewwillappear is called when the view is about to be added to the window: method
  
4> when the view has been added to the window, the controller's viewdidappear is called: method
  
5> if the controller's view is about to be removed from the window, the controller's viewwilldisappear is called: method
  
6> if the controller's view has been removed from the window, the controller's viewdiddisappear is called: method
  
7> if the controller receives a memory warning, the controller's Didreceivememorywarning method is called
  
The default implementation of the Didreceivememorywarning method is that if the controller's view is not displayed in the window, that is, Controller.view.superview is nil, the system destroys the controller's view.
  
8> the controller's Viewdidunload method is called after the destruction is complete
  
9> if the controller's view was previously destroyed because of a memory warning and now needs to access the controller's view again, the previous steps are repeated to initialize the view
  
Vi. Summary
  
This article is a bit more, but it's our first iOS app to get started with iOS, and from this simple program we can learn about the life cycle of a program and the relationships between them. We also learned about the interface layout features and two methods in iOS. The following section continues with the contents of the controller in iOS.

iOS Fury Road---what you can learn in the first app of iOS

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.