IOS study Note 6-First Response object First (First Responder)

Source: Internet
Author: User
Tags uicontrol

Finally, I waited for the iOS project and handed over the PHP project. But iOS learning has been stuck for so long? No matter where it is, let's make up for two days. You can see where it is, and then learn while doing it.

 

I learned about the front-end UI today. Let's talk about the response Object. The reason for this is that the chapter after the tutorial will also involve touch, so I will not write it if I haven't learned it yet.

 

1. UIResonder

All controls (such as TextBox) in C # are inherited from the Control class. The inheritance relationships of the Control class are as follows:

System. Object

System. externalbyrefobject

System. ComponentModel. Component

System. Windows. Forms. Control

The UI classes in iOS also have similar inheritance relationships.

For example, UITextField is inherited from UIControl; UIControl is inherited from UIView, UIView is inherited from UIResponder, and UIResponder is inherited from NSObject.

For specific architecture, see:

Http://developer.apple.com/library/ios/#documentation/general/conceptual/Devpedia-CocoaApp/Responder.html

 

 

UIResponder is the class in the UIKit framework (Mac OS X Cocoa corresponds to the AppKit framework ).

 

2. First Response object

In the response object of the application, one will become the first response object.

What is the difference between the first response object and other response objects? There is no difference between common touch events. Even if I set a button as the first response object, when I click another button, it will still respond to other buttons without giving priority to the first response object.

The difference between the first response object is that it is responsible for handling events unrelated to the location of the screen, such as shaking.

In Apple's official document, the first response object is the object in the window, which the application considers to be the most suitable for event processing.

A class can have only one class leader. Only one response object in the application can be the first response object.

 

3. Become and cancel the first response object.

To be the first response object, you also need a View for self-Recommendation:

- (BOOL) canBecomeFirstResponder{    returnYES;}

 

If this section is missing, a view cannot be the first response object even if [view becomeFirstResponder] is used... Is it sweet? Well, that's not the reason. By default, most views only care about events associated with themselves, and (almost) always have the opportunity to handle these events. Taking UIButton as an example, when a user clicks a UIButton object, the object will receive the specified action message regardless of the view of the current first response object. Is the first response object thankless... Therefore, only a certain UIResponder explicitly indicates that it is willing to become the first response object. (I don't know what the design is based on... Security ?)

 

In the first response object, different objects may have some special performances. For example, when UITextField is used, a keyboard is called up.

The first response object may also be dismissed. Send a resignFirstResponder to persuade you to leave.

 

4. task of the first response object

As mentioned earlier, the first response object can handle shaking. Let's look at an example:

 

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{    if(motion == UIEventSubtypeMotionShake)    {        NSLog(@"Device is beginning to shake");        [selfsetCircleColor:[UIColorredColor]];        [selfsetNeedsDisplay];    }}

 

Some actions are triggered when the shaking starts.

 

5. Get the current first response object

From this discussion: http://stackoverflow.com/questions/1823317/get-the-current-first-responder-without-using-a-private-api

The guy who asked the question uses the following method to get it

UIView * firstResponder = [keyWindow inclumselector: @ selector (firstResponder)];

The result was called back by Apple, indicating that a non-public API was used...

So this guy had to use recursion:

implementationUIView (FindFirstResponder)- (UIView *)findFirstResponder{    if (self.isFirstResponder) {                return self;         }    for (UIView *subView in self.subviews) {        UIView *firstResponder = [subView findFirstResponder];        if (firstResponder != nil) {            return firstResponder;        }    }    return nil;} @end

 

Well... So if necessary, either specify a fixed first response object or get a global variable to save the current first response object?

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.