In-depth analysis of iPhone development application Delegation

Source: Internet
Author: User

IPhoneDevelop applicationsDelegateIs the content to be introduced in this article, mainly describes the applicationDelegate.IPhoneThe software stack consists of several layers, while the application is located at the highest abstraction layer in the stack, and the operating system layer of the system core service) is located at the lowest layer. This includes the media layer, Cocoa Touch layer, and core service layer. However, during application development, we mainly deal with the object-oriented abstraction provided by the GUI framework and the Cocoa Touch layer.

The Foundation framework of Cocoa includes basic data and basic tools. Almost all Cocoa classes are inherited from a root object NSObject. This mainly describes the application layer eventsDelegateAnd Application lifecycle.

To describe the application, start with main.

 
 
  1. int main(int argc,char *argv[]}{  
  2.            NSAutoreleasePool *pool  = [[NSAutoreleasePool alloc] init];  
  3.            int retVal = UIApplicationMain(argc,argv,nil,nil);  
  4.            [pool release];  
  5.            return retVal;  

This is a typical C-style main function. Its core function is to call the UIApplicationMain function to start the event loop and start running the application. Pay attention to the third and fourth parameters of the main function, these two parameters indicate the main class of the application and the application delegate. This delegate is a class that handles application lifecycle management events. If nil is specified, these class objects need to be loaded from a nib file. In the Info. plist file, a nib file is specified. By default, it is MainWindow. xib. The next thing is that UIkit looks for the Application delegate in the xib file.

In the nib file opened with IB), you can see several icons, one of which is the icon delegated by the application. The others seem to be the View Controller icon and a window icon. In addition, the File's Owner has the object of the current nib File) and the First Responser instructs the component to receive the input of the user First) Two icons.
Then how does the application connect the delegate object to the application?

The iPhone application has a window object, and the application delegate has two connections. One is the window object connected to the nib file, and the other is the View Controller connected to the nib file. In general, we can control our display by customizing the behavior of the View Controller.

The whole process is like this. The system first calls the main function, and then the main function calls the UIAplicationMain function. This function uses Info. the plist file is used to find the main nib file of the application. In this file, find a delegate of the application. The delegate has two connections, which can be connected to a window and View Controller. The view controller can then customize our display behaviors by using code or nib files. This is the whole process of starting the application.

What can I do next?

The life cycle of an application is controlled by the life cycle method. These Life Cycle methods are related to the application delegate. Of course, they are also related to the View Controller loaded by the application. For example, if the application has just been started, call the delegate applicationDidFinishLaunching: method.
Why do we need to delegate?

A simple idea is that an object can have a single delegate object and can call the event Method of the delegate object when some events occur. The delegate object is like a professional expert. It always says: It's okay. When this event happens, just tell me. Another object that receives events has many other business things to handle: I really don't have time to handle these things, and I don't know what I should do, so I'll give it to you.

DelegateThe object is defined by UIApplicationDelegate. Therefore, if you want to delegate an application to a class, you must declare it in the header file to implement this protocol, and then in the implementation of the class, all required methods are implemented.

Summary: in-depth analysisIPhoneDevelop applicationsDelegateI hope this article will help you!

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.