Uiapplication object and its proxy uiapplicationdelegate

Source: Internet
Author: User

During the development process, we need some global objects to connect each part of the program. The most important of these global objects is the uiapplication object. But in actual programming, we do not deal with the uiapplication object directly, but with its proxy.

Uiapplication
Is the start of the iPhone application and is responsible for initializing and displaying the uiwindow, and is responsible for loading the first uiview of the application to the uiwindow form.
Another task of uiapplication is to help manage the lifecycle of an application.
The proxy class of uiapplicationdelegate to perform this task. Although uiapplication is responsible for receiving events
Uiapplicationdelegate determines how the application responds to these events. events that can be handled by uiapplicationdelegate include
Such as program startup and shutdown, and system events (such as incoming calls and note warnings). This article describes how to load the application's uiview to the uiwindow and how to use
Uiapplicationdelegate handles system events.

Connect
For uiapplication readers, there is no need to modify it. You only need to know That uiapplication receives system events, and how to write code to handle these system events is
Programmer's work. To process system events, you need to write a class that inherits from the uiapplicationdelegate interface, while the uiapplicationdelegate Interface
Provides lifecycle functions to process system events of applications and applications.

For example
If you create a project using the xcode template, xcode will create a class inherited from uiapplicationdelegate for the programmer, but it will not automatically inherit from
Optional event processing functions of uiapplicationdelegate. If you create a project named "testuiapplication", xcode will
Create the testuiapplicationappdelegate. h and testuiapplicationappdelegate. m Files. File Declaration
As follows:
@ Interface testuiapplicationappdelegate: nsobject <uiapplicationdelegate>
The uiapplication of the application is defined in the mainwindow. XIB file, and there is a uiapplicationdelegate reference as the outlet.

IPhone
It is not a multi-task operating system, so applications are easily disturbed. For example, an incoming call may cause the application to lose focus. If a call is received at this time, the application will automatically stop running. Also
There are many other similar events that will cause iPhone applications to lose focus and call the delegate class before the application loses focus.
Applicationwillresignactive () method, which is called when the application obtains the focus again.
Applicationdidbecomeactive () method. For example, the lock screen will call the delegate class when running the application.
Applicationwillresignactive () method, and when the screen is unlocked, it will call
Applicationdidbecomeactive () method.

Another
Another important method is applicationdidreceivememorywarning (), because the iPhone device only has limited memory.
If the program is allocated with too much memory, the operating system will terminate the operation of the application, but before the termination, the operating system will call
Applicationdidreceivememorywarning () method to warn the application. After the uiapplication receives this event, it will pass it
The applicationdidreceivememorywarning () method of the delegate class. The delegate class can release the memory in this method to prevent the operating system from forcing
Terminate the application.

Now let's take a look at the functions of the methods to be implemented defined in the Protocol:
1.-(void) applicationwillresignactive :( uiapplication *) Application
Note: When an application is about to run in an inactive state, during this period, the application does not receive messages or events, such as calls.
2.-(void) applicationdidbecomeactive :( uiapplication *) Application
Note: When the application enters the activity status for execution, this is exactly the opposite of the method above.
3.-(void) applicationdidenterbackground :( uiapplication *) Application
Note: This API is called when a program is pushed to the background. So to set the background to continue running, you can set it in this function.
4.-(void) applicationwillenterforeground :( uiapplication *) Application
Note: This method is called when the program is to return to the foreground from the background.
5.-(void) applicationwillterminate :( uiapplication *) Application
Note: A program is called to exit. It is usually used to save data and clean up before exiting. You need to set the key value of uiapplicationexitsonsuspend.
6.-(void) applicationdidreceivememorywarning :( uiapplication *) Application
Note: iPhone devices only have limited memory. If too much memory is allocated to the application, the operating system will terminate the operation of the application. This method will be executed before the termination, you can usually clean up the memory here to prevent the program from being terminated.
7.-(void) applicationsignificanttimechange :( uiapplication *) Application
Description: executed when the system time changes
8.-(void) applicationdidfinishlaunching :( uiapplication *) Application
Note: It is executed after the program is loaded.
9.-(void) Application :( uiapplication) Application willchangestatusbarframe :( cgrect) newstatusbarframe
Description: This parameter is executed when the statusbar box is about to change.
10.-(void) Application :( uiapplication *) Application willchangestatusbarorientation:
(Uiinterfaceorientation) newstatusbarorientation
Duration :( nstimeinterval) Duration
Description: This parameter is executed when the orientation of the statusbar box is about to change.
11.-(bool) Application :( uiapplication *) Application handleopenurl :( nsurl *) URL
Note: When executed through a URL
12.-(void) Application :( uiapplication *) Application didchangestatusbarorientation :( uiinterfaceorientation) oldstatusbarorientation
Note: After the statusbar box direction changes, execute
13.-(void) Application :( uiapplication *) Application didchangesetstatusbarframe :( cgrect) oldstatusbarframe
Note: After the statusbar box changes, execute

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.