Page jumps and auto-hide of the virtual keyboard

Source: Internet
Author: User

Premise: There are at least two view controllers.


Precautions:
Animate the Jump
Uitextfield the location of the virtual keyboard when typing (the overall layout moves up and the virtual keyboard is automatically hidden)


The functions implemented:
A: On the first page jump to the second page, then the second page click on the button to return
B: On the first page there is a text control, click on a button, pass to the second page, and then show to the second
Label above the page:


Effect Show:



Steps:


1. Create a new two class and tick xib to load one of the views in the Appdelegate implementation file

Create a new view controller    mainviewcontroller *myview = [[Mainviewcontroller alloc]init];    Let the new view controller join the view of the root class    self.window.rootViewController = MyView;


2. Build the layout and define the method:
The header file code is as follows:

#import <UIKit/UIKit.h> #import "DetailViewController.h" @interface mainviewcontroller:uiviewcontroller< uitextfielddelegate>{    //The 2 page is declared as a member variable, which facilitates the invocation of multiple methods of this class.    Detailviewcontroller *dail;} Click Yes page jump-(ibaction) Enter: (ID) sender;//the input text to pass in the past, on 2 page display-(ibaction) Image: (ID) sender;//Text control @property (Strong, nonatomic) Iboutlet Uitextfield *sent; @end


3. In the implementation file, the implementation code for the method is as follows:

-(Ibaction) Enter: (ID) Sender {     //0, default. 1. Flip 2. Fade  3. Page    Dail.modaltransitionstyle = 2;       Jump Operation    [self presentviewcontroller:dail animated:yes completion:nil];    } -(Ibaction) Image: (ID) Sender {   //jump and send text    [self presentviewcontroller:dail animated:yes completion:nil];     Dail. Get.text = self. Sent.text;}


The header file code for the 4.2 page is as follows:

#import <UIKit/UIKit.h> @interface detailviewcontroller:uiviewcontroller//Click, return-(Ibaction) return: (ID) sender;//is used to accept the text passed over: @property (strong, nonatomic) Iboutlet UILabel *get; @end2 page implementation file by code as follows: (partial method implementation code)-(ibaction) return: (ID) Sender {       //Click to return operation    [self Dismissviewcontrolleranimated:yes completion:nil];}


Resolve Issue 1:
If the text box is in the middle of a page, when we enter it, the virtual keyboard hides the text box. We can
To move the interface collectively up,


Prerequisite: First we must abide by the Uitextfielddelegate protocol and invoke the member method inside:
Code implementation:

-(void) textfielddidbeginediting: (Uitextfield *) textfield{    //Add an animation effect: One second to move the view up, because the keyboard appears to move up, if your view jumps between    //assigned coordinates, there will be a vacancy in the middle;    [UIView animatewithduration:1 animations:^{    //Make the y-coordinate of the page change to-260. Move up.        self.view.frame = CGRectMake (0, -260, Self.view.frame.size.width, self.view.frame.size.height);    }];    }


Resolve Issue 2:

If we have finished typing, we need to click the button to jump, found that the current button has been hidden by the virtual keyboard
How can the mouse click on the blank space, the virtual keyboard will be automatically hidden: this time to rewrite UIView method:
To let us enter the end of the time, let the input box cancel the first responder. So that the keyboard can be hidden:


Premise:
Sets the TextField proxy object to the current class object:
The code is as follows:

-(void) viewdidload{    [Super Viewdidload];    Dail = [[Detailviewcontroller alloc]init];        Sets the Uitextfield proxy to the current class object self    . Sent.delegate = self;    Do any additional setup after loading the view from its nib.}

The following code is automatically hidden by the virtual keyboard:


The code is as follows:
Touch time: The method of rewriting the system: Click on the blank space, the virtual keyboard disappears//Let the entire text input box cancel the first responder, so that all controls of the keyboard hide-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{    //nslog (@ "touch");    [Self. Sent Resignfirstresponder];}


Description Complete :


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Page jumps and auto-hide of the virtual 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.