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.
- int main(int argc,char *argv[]}{
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- int retVal = UIApplicationMain(argc,argv,nil,nil);
- [pool release];
- 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!