Parsing the FirstResponder first Response object in IOS development _ios

Source: Internet
Author: User
Tags reserved uicontrol uikit

1. Uiresonder

All controls in C # (for example, a TextBox) inherit from the control class. The control class inherits relationships such as
Under

Copy Code code as follows:

System.Object

System.MarshalByRefObject

System.ComponentModel.Component

System.Windows.Forms.Control


There are similar inheritance relationships for the UI classes in iOS.

For example, for Uitextfield, inherited from Uicontrol;uicontrol inherits from Uiview,uiview inherits from Uire
Sponder,uiresponder inherited from NSObject.

Specific schemas can be found in:

http://developer.apple.com/library/ios/#documentation/GENERAL/CONCEPTUAL/DEVP
Edia-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 application response object, there will be a first response object.

What is the difference between the first response object and the other response object? There is no difference between ordinary touch events. Even
I set a button to be the first response object, and when I clicked on the other buttons, I would respond to the other buttons without
Gives priority to responding to the first Response object.

The first response object differs in that it is responsible for handling events that are unrelated to the screen location, such as shaking.

Apple's official document says that the first response object is the object in the window that the application considers best suited to handle the event

A class can only have one monitor, only one response object in the applied response object becomes the first response object.

3. Become and cancel the first response object.

To be the first responders, you need to have a view to volunteer:

Copy Code code as follows:

-(BOOL) Canbecomefirstresponder
{
Returnyes;
}

Without this paragraph, even with [view Becomefirstresponder], you can't make a view the first response.
Object... A strong twist of melon is not sweet? Well, that's not the reason. Most views default only to those that are associated with them.
Events, and (almost) always have the opportunity to handle these events. Take UIButton as an example, when the user clicks a UiB
When you utton an object, the object receives the specified action message, regardless of which view the current first response object is.
When the first Response object thankless ... So only by a certain uiresponder clearly that they are willing to become
Is the first Response object. (I don't know what to consider on the design.) Safety? )

When you first respond to an object, different objects may have some special performance. For example, when Uitextfield is on the
, you will be able to pull out a small keyboard.

The first responders are also likely to be dismissed. Send a Resignfirstresponder, you can persuade.

4. Tasks of the first Response object

Just said the first response object can handle shaking. Let's look at an example:


Copy Code code as follows:

-(void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *) event
{
if (motion = = Uieventsubtypemotionshake)
{
NSLog (@ "Device is beginning to shake");
[Selfsetcirclecolor:[uicolorredcolor]];
[Selfsetneedsdisplay];
}
}

Some behavior is triggered when the shaking starts.

5. Get the current first response object

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

The guy who asked the question used the following way to get

Copy Code code as follows:

UIView *firstresponder = [Keywindow
Performselector: @selector (FirstResponder)];

The result is the Apple called back, said the use of Non-public API ...

So the guy had to use recursion:

Copy Code code as follows:

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

The release problem of 6.View FirstResponder
There was a problem today when I hid a uitextfield that was accepting user input, the keyboard doesn't go away, and the keyboard still accepts user input, again showing the textfield that all input is still transferred to the TextField in the hidden state. So check the official information to find the following explanation:

Important If You hide a view this is currently the "responder", the view does not automatically resign its-respo NDEr status. Events targeted at the responder are still delivered to the hidden view. To prevent this from happening, your should force your view to resign the the ' the ' the ' the ' the ' the ' the ' the ' the ' the ' the '

This means that if this view is the current first responder, hiding the view does not automatically discard the identity of its first responder, and will continue to accept the message as the first responder. We can force the view to discard the first responder's identity by manually invoking Resignfirstresponder before hiding the view.

See below for a small example:

Copy Code code as follows:

SvTestFirstResponder.h

//
SvTestFirstResponder.h
//
Created by Maple on 3/15/12.
Copyright (c) smileevday. All rights reserved.
//
When a view is the current responder, calling its hidden method does not automatically discard the first responder identity, and all messages are still sent to this view
You can return to normal message delivery by forcing the first responder to hidden before the
//

#import <UIKit/UIKit.h>

@interface Svtestfirstresponder:uiview {
Uitextfield *_inputfield;
}

@end


Copy Code code as follows:

Svtestfirstresponder.m

//
Svtestfirstresponder.m
//
Created by Maple on 3/15/12.
Copyright (c) smileevday. All rights reserved.
//

#import "SvTestFirstResponder.h"

@interface Svtestfirstresponder ()

-(void) Hiddeninputview: (ID) sender;
-(void) Showinputview: (ID) sender;

@end

@implementation Svtestfirstresponder

-(ID) initWithFrame: (CGRect) frame
{
self = [super Initwithframe:frame];
if (self) {
Initialization code

_inputfield = [[Uitextfield alloc] Initwithframe:cgrectmake (0, 0, 200, 50)];
_inputfield.center = Cgpointmake (160, 50);
[_inputfield Setfont:[uifont systemfontofsize:24]];
_inputfield.text = @ "input you text";
_inputfield.clearsonbeginediting = YES;
_inputfield.contentverticalalignment = Uicontrolcontentverticalalignmentcenter;
_inputfield.borderstyle = Uitextborderstyleroundedrect;
[Self Addsubview:_inputfield];
_inputfield.autoresizingmask = Uiviewautoresizingflexibleleftmargin | Uiviewautoresizingflexiblerightmargin;

UIButton *hiddenbtn = [UIButton buttonwithtype:uibuttontyperoundedrect];
Hiddenbtn.frame = CGRectMake (0, 0, 115, 40);
Hiddenbtn.center = Cgpointmake (80, 110);
[Hiddenbtn settitle:@ "Hide TextField" forstate:uicontrolstatenormal];
[Hiddenbtn addtarget:self Action: @selector (Hiddeninputview:) forcontrolevents:uicontroleventtouchupinside];
[Self addsubview:hiddenbtn];
Hiddenbtn.autoresizingmask = Uiviewautoresizingflexiblerightmargin;
HiddenBtn.titleLabel.lineBreakMode = uilinebreakmodetailtruncation;

UIButton *showbtn = [UIButton buttonwithtype:uibuttontyperoundedrect];
Showbtn.frame = CGRectMake (0, 0, 115, 40);
Showbtn.center = Cgpointmake (240, 110);
[Showbtn settitle:@ "show TextField" forstate:uicontrolstatenormal];
[Showbtn addtarget:self Action: @selector (Showinputview:) forcontrolevents:uicontroleventtouchupinside];
[Self addsubview:showbtn];
Showbtn.autoresizingmask = Uiviewautoresizingflexibleleftmargin;
ShowBtn.titleLabel.lineBreakMode = uilinebreakmodetailtruncation;
}
return self;
}

/*
Only override drawrect:if you perform custom drawing.
An empty implementation adversely affects performance during.
-(void) DrawRect: (cgrect) rect
{
Drawing Code
}
*/

-(void) Hiddeninputview: (ID) sender
{
_inputfield.hidden = YES;
}

-(void) Showinputview: (ID) sender
{
_inputfield.hidden = NO;
}

@end

In this simple example, when the input box enters the acceptance user input state, clicking the Hide button will not disappear and will continue to receive user input and upload the user input to the TextField. When you click the Show button later, you will find that all text entered in the hidden state has been successfully received. We can modify the Hide method as follows:

Copy Code code as follows:

-(void) Hiddeninputview: (ID) sender

{

if (_inputfield.isfirstresponder) {

[_inputfield Resignfirstresponder];

}

_inputfield.hidden = YES;

}

This allows you to force the first responder to be released before it is hidden. This is a more detailed question, but sometimes it may be that this kind of detail can lead to some inexplicable problems, adding a forced release of the first responder before hiding some view that might be the first responder may help us avoid some strange problems, And there's hardly any overhead, why not.

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.