-ios Training, iOS learning------- -type technology blog, look forward to communicating with you! ------------iOS Developer's Pure code keyboard quits (very simple)
iOS Developer's Pure code keyboard quits
Before talking about the keyboard exit several times, but recently began to work on the project is stuffy, because too much, I do know that there are many methods can be achieved, and let me affect the deepest is endediting, but because there is TextView, There are textfield and they have different methods, although the author now understand, but do not know when do not remember, and although feel very simple now feel very simple appearance, but for not really clear people, is also a painful thing, haha!
Today to introduce how in the case of pure Code, exit (hidden) keyboard, xib and storyboard case here does not explain, I hope to see the author of the article written earlier.
One: Uitextfield
About Uitextfiel personal feeling and a lot of methods, but recently developed I use the most is the two, according to and already in the company to work in the chat, now the most used in the development of these two, of course, if you want to learn other methods or better methods, Or you might want to put a force in front of a normal programmer.
1) Click Return
About Click Return is to implement an agent method that is
-(BOOL) Textfieldshouldreturn: (uitextfield*) TextField
{
[TextField Resignfirstresponder];
return YES;
}
But before you do, you need to get out of the keyboard. Implement an agent in the same class that is uitextfielddelegate:
@interface Icocosfbviewcontroller ()
After implementing this agent is not finished, but also to define a property:
@property (nonatomic, weak) Uitextfield*text;
Finally, follow this agent in the appropriate startup method:
_text.delegate = Self;
Then all this is OK, in any non-method line implementation file input the above proxy method can be achieved click Return to exit the keyboard.
2) Touch the screen
This touch screen is simple, say first you need to define a property text (IBID.). You think if you want to quit the keyboard, you can be sure that when you touch the screen keyboard immediately exit, you can not sea let him wait for a second, what you will? Then you're just going to have to wait.
Now that we have to exit immediately, we know in the proxy method of the screen touch that there is a way to scatter the began method:
Yes, that's him.-(void) Touchesbegan: (Nsset *) touches withevent: (uievent*) event we just need to implement the code to exit the keyboard here, there are two types of code that exits the keyboard, But you think that since you want to quit the keyboard must be the introduction of editing, unless you have other places to enter, it is not called out of the keyboard, so we use the most is the direct end, in this method makes the text directly end edit, end. End ... Still do not understand, forget to show you the code directly, do not understand or go home farming!
-(void) Touchesbegan: (Nsset *) Toucheswithevent: (Uievent *) event
{
[Self.text Endediting:yes];
}
Two: TextView
About the TextView method is basically the same as the above knowledge agent and proxy method is not the same, but the idea is completely identical, so I do not do too much introduction.
1) Return
Implementing an agent approach that's
-(BOOL) TextView: (Uitextview *) Textviewshouldchangetextinrange: (nsrange) Range Replacementtext: (nsstring*) text
{
if ([text isequaltostring:@ "\ n"]) {
[TextView Resignfirstresponder];
return NO;
}
Returnyes;
}
But before you do, you need to get out of the keyboard. Implement an agent in the same class that is uitextviewdelegate:
@interface Icocosfbviewcontroller ()
After implementing this agent is not finished, but also to define a property:
@property (nonatomic, weak) Uitextview*text;
Finally, follow this agent in the appropriate startup method:
_text.delegate = self;
。。。。。
。。
。
2) Touch the screen
。。。。
。。。
。。
。
-(void) Touchesbegan: (Nsset *) Toucheswithevent: (Uievent *) event
{
[Self.text Endediting:yes];
Finally, everyone concludes that Uitextfield and Uitextview exit the keyboard at the same point and different points:
In fact, the two controls exit the keyboard method 80% is the same, such as if you are to touch the screen to exit so is to implement this method
-(void) Touchesbegan: (Nsset *) Toucheswithevent: (Uievent *) event
{
[Self.text Endediting:yes];
}
Then, then there is no then ...
The implementation of proxy method is different from the proxy name, attribute name, and the proxy method name is different, just uitextview this proxy method used a matching string, \ n, do not ask this is Satan, I do not know .... Left to see right or not to understand, \->n I guess may be called you go to sleep meaning!.
If we can match, it's just the TextField method,
。。。
。
。
。
"Good programmer notes sharing"--ios development of the pure Code keyboard exit