IOS development-Event Response

Source: Internet
Author: User

Events can appear on the iPhone in three main ways: Action, delegate event, and notification.

The iPhone event response comes from the uiresponder object, while the iPhone notification comes from the nsicationicationcenter. You don't have to worry about how to access the method and attributes of the responder, because the uiresponder object is the parent of most uikit objects, but the nsicationicationcenter requires special access.

I. events and actions

Most users input an event on the iPhone that is placed in the responder chain. The responder chain is a set of object links. Most of them are extended up through the view hierarchy. Any input is first captured by the first responder. It is generally an object that interacts directly with the user. If this object cannot parse the input, it will send the input to its superview (for example, the tag may send the input to its full screen view) and then to its superview, continuously throw (for example, up to the view and then up to the View Controller ). If the input goes up along the view hierarchy to the window object, it is then sent to the application itself and finally passed to the application delegate.

 

Any of these objects can choose to process an event, which will stop the delivery of the responder chain. According to the standard MVC mode, the event response is usually constructed into the uiviewcontrollers object, which is far away from the responder chain.

 

For any type of uicontrol object, such as buttons, Slider, and switch, events are converted into actions. When an event is reported to the screen, the action reports the operation of the control, making it easier to read. The action follows a slightly different response hierarchy.

 

Ii. Delegation and data sources

Delegate: You can send events to objects that are not the first responder. That is, an object (usually a View Controller) processes events of another object (usually a view. It is a close relative of the data source, and the data source is also the data settings and control for one object to process another object.

 

The delegate and data source are respectively controlled by one protocol, which is a set of methods for entrusting and the data source to agree to the response. For example, a table delegate may have to respond to a table. This method will warn you when the table row is selected. Similarly, a table data source may be used to describe the appearance of all rows in a table.

 

Delegates and data sources can perfectly adapt to the MVC pattern used by objective-C, because they allow views to forward their work to their controllers without worrying about the location of these objects in the response chain.

 

3. Notifications

Standard Event Response and delegation represent two ways to warn an object about a standard event (for example, a finger touch screen. You can also use the third method-Notification-to plan many different types of activities, such as changing the iPhone direction or disabling network connections.

 

Object Registration uses nsicationicationcenter to receive certain types of notifications and process them accordingly.

 

All notifications occur through nsicationicationcenter. To use this notification, you must create a copy of the shared object:

[Nsnotificationcenter defacenter center]

Then, you can use the-(void) addobserver :( ID) Observer selector :( SEL) aselector name :( nsstring *) aname object :( ID) anobject method to request a notification. The observer is an object (usually self) and will receive the notification method. selector: is the method that will be called in the observation program. Name: is the name of the notification (will appear in the class reference), object: is the parameter (but it is usually set to nil) from which you want to restrict the objects used to receive notifications );

For example, you can use the following code to receive the Termination notification of our application uiapplicationwillterminatenotification:

[[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (willterminate :) name: uiapplicationwillterminatenotification object: Nil];

In short, notifications generally take the following four steps: first, read the appropriate reference and learn that there is a notification. Second, you may need to enable the notification explicitly (this is true for uiapplicationwillterminatenotification). Third, write a method to respond to the notification (in the above example, use:-(void) willterminate :( nsnotification *) notification). Fourth, use nsicationicationcenter to connect notifications to methods.

 

The notification system has more powerful functions. You can not only set multiple observers, but also post your own notifications.

 

 

 

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.