[Good programmer notes sharing] -- exit the keyboard with pure code and exit the keyboard with programmer notes

Source: Internet
Author: User

[Good programmer notes sharing] -- exit the keyboard with pure code and exit the keyboard with programmer notes

IOS training ------ my C Language notes. I look forward to communicating with you!

 

There are many methods to exit the keyboard in iOS development, and I have also learned a lot, including the exit of various non-pure code interfaces.

However, I was bored when I started the project recently. Because there are too many methods, I do know that there are many methods that can be implemented, and EndEditing is the most influential one, but because there is textView, there are TextField and they have different methods. Although I understand it now, I do not know when I will not remember it, and although I feel very simple, I feel very simple now, but for those who have not really figured it out, it is also a painful thing, haha!

 

In fact, if the code-only interface is used on the Xib or Storyboard, it is still the same idea, but the author encountered problems in the code-only interface during development, so it has been named for a long time.

Next we will introduce how to exit (hide) the keyboard with pure code. In the case of Xib and StoryBoard, we will not explain it here (based on this idea ).


I. UITextField


There are many methods in UITextFiel, but the two methods I used most recently are based on chats with colleagues who have already worked in the company, these two methods are the most used in development. Of course, if you want to learn other methods or more awesome methods, or you want to force it in front of a common programmer.

1) Click Return.


Click Return to implement a proxy method, that is
-(BOOL) textFieldShouldReturn :( UITextField *) textField
{
 

[TextField resignFirstResponder];
Return YES;
}
But before that, you need to implement a proxy in the class where you want to exit the keyboard, that is, UITextFieldDelegate:
@ Interface iCocosFBViewController ()
After the proxy is implemented, an attribute must be defined:
@ Property (nonatomic, weak) UITextField * text;
Finally, follow the proxy in the corresponding startup method:
_ Text. delegate = self;

Then all this is OK. In the implementation file of any non-Method Line, enter the proxy method above to implement the Return operation and exit the keyboard.


2) Touch Screen
This touch screen is simple. First, you need to define an attribute text (same as above ). If you want to exit the keyboard, you can decide to exit immediately after you touch the screen keyboard. You cannot let him wait for a second. What do you want to do? Then you will keep waiting.
Now that you want to exit immediately, we know in the proxy method of the screen touch that there is a method to scatter the Began method:
By the way, it is his-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event. We only need to implement the Code to exit the keyboard here, there may also be two types of code to exit the keyboard here, but you think that since you want to exit the keyboard, it must be about editing. Unless you want to enter anything else, you will not exit the keyboard, therefore, the most commonly used method is direct End. In this method, the text is directly edited by End and End .. End... I haven't understood it yet. I want to show you the code directly. If you can't understand it, go back to farming!
-(Void) touchesBegan :( NSSet *) toucheswithEvent :( UIEvent *) event
{
[Self. text endEditing: YES];
} 2: TextView the TextView method is basically the same as the above knowledge proxy and proxy method, but the idea is exactly the same, so I will not introduce it too much.

1) Return
Implementing a proxy method is
-(BOOL) textView :( UITextView *) textViewshouldChangeTextInRange :( nsange) range replacementText :( NSString *) text
{
If ([text isEqualToString: @ "\ n"]) {
[TextView resignFirstResponder];
Return NO;
}
ReturnYES;
}
But before that, you need to implement a proxy in the class where you want to exit the keyboard, that is, UITextViewDelegate:
@ Interface iCocosFBViewController ()
After the proxy is implemented, an attribute must be defined:
@ Property (nonatomic, weak) UITextView * text;
Finally, follow the proxy in the corresponding startup method:
_ Text. delegate = self;

.....
..
.

2) Touch Screen
....
...
..
.

-(Void) touchesBegan :( NSSet *) toucheswithEvent :( UIEvent *) event
{
[Self. text endEditing: YES];
}



In conclusion, the similarities and differences between UITextField and UITextView exit the keyboard are as follows:
In fact, the two methods for controlling the exit from the keyboard are the same as 80%. For example, if you want to touch the screen to exit, this method is implemented.
-(Void) touchesBegan :( NSSet *) toucheswithEvent :( UIEvent *) event
{
[Self. text endEditing: YES];
}

Then, there will be no more ......

The agent name, attribute name, and proxy method name are different, except that the proxy method of UITextView uses a matching string \ n. Don't ask if this is a scatter, I don't know either .... I still don't understand it at the left and right, \-> n. I guess it might mean asking you to go to bed !.
If it can be matched, it will be the TextField method ,,,

...
.
.
.

Of course, there are also some background exit. Here we will give you an idea to put a UIButton directly on it and set it to the interface size and transparency. Finally, set its UserEnale or userInteractionEnabled to NO, click events for a long time.

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.