iOS Development--oc & Pure Code exit keyboard

Source: Internet
Author: User

On the iOS development keyboard exit, in fact there are many methods, and I also learned a lot, including a variety of non-pure Code interface exit.

In fact, the introduction of pure code interface if the use of Xib storyboard above or the same idea operation, but the author in the development of the time is in the pure Code interface encountered problems, so long to this name.

The following describes how in the case of pure Code, exit (hidden) keyboard, xib and storyboard circumstances here will not explain (according to this idea).


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

1-(BOOL) Textfieldshouldreturn: (Uitextfield *) textField2 {3  4 5    [TextField resignfirstresponder];6     Return  Yes;7}

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 *) touches withevent: (uievent *) event{    [Self.text endediting:yes];}


Two: TextView about the TextView method is basically the same as the above knowledge agent and agent method is not the same, but the idea is exactly the same, so I do not do too much introduction.

1) Return
Implementing an agent approach that's
1-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) Text2 {3     if ([text isequaltostring:@ "\ n"]) {4         [TextView resignfirstresponder];5         return no;6     }7     return YES;8}


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 *) touches withevent: (uievent *) event{    [Self.text endediting:yes];}


Finally, everyone summed up that is Uitextfield and Uitextview exit the keyboard 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 *) touches withevent: (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,

。。。




Of course, there are some touch background exit, here is a thought, directly above put a UIButton set to the interface size, and set to transparent, and finally set his userenale or userinteractionenabled for No, and then realize the Click event Long can. Finally, here's a quick introduction to how Swift launches the keyboard (just a brief introduction):

The first is to follow the proxy, is to implement the keyboard hiding in the proxy method

    1. 1     override func didreceivememorywarning () {2         super.didreceivememorywarning () 3         //Dispose of any resources That can is recreated. 4     } 5     //The first method. Implement keyboard hiding with proxy 6     func Textfieldshouldreturn (Textfield:uitextfield), Bool {7         if (TextField = = Self.text) {8
           
            9             Textfield.resignfirstresponder ()//            Self.view.becomeFirstResponder ()         }12             return true;     }14     
           

The second method implements the keyboard hiding

      Override Func Touchesbegan (Touches:nsset, withevent event:uievent) {        self.text.resignFirstResponder ()        Self.pwdtext.resignFirstResponder ()    }

Perhaps you have seen the following method:

1th method of collecting keyboard

1-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event2 {3 [[uiapplication sharedapplication] sendaction:@ Selector (resignfirstresponder) To:nil From:nil forevent:nil];4}

2nd Method of collecting keyboard

-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{[[[uiapplication sharedapplication] KeyWindow] Endediting:yes];}

iOS Development--oc & Pure Code exit keyboard

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.