iOS Development Uitextfield

Source: Internet
Author: User

Uitextfield shows some editable content and has some interaction with the user. For example, when you press return on a virtual keyboard, it is typically associated with a keyboard-hidden event. Most of the Uitextfield states have corresponding methods in the Uitextfielddelegate protocol.

Initialization of Uitextfield and some properties
Name input field Uitextfield *namefield = [[Uitextfield alloc] Initwithframe:cgrectmake (100, max.)];namefield.tag    Namefield.delegate = self;    Default text Namefield.placeholder = @ "name";    Namefield.font = [Uifont systemfontofsize:16.0f];    Namefield.textcolor = [Uicolor blackcolor];    The background picture of the input box (you can also choose to set the background color) Namefield.background = [UIImage imagenamed:@ "Textfieldbackgroundimage"];    Namefield.backgroundcolor = [Uicolor Lightgraycolor];    Clear button Namefield.clearbuttonmode = uitextfieldviewmodealways;    Keyboard type namefield.keyboardtype = Uikeyboardtypedefault;    [Self.view Addsubview:namefield]; Telephone input domain Uitextfield *phonefield = [[Uitextfield alloc] Initwithframe:cgrectmake (NAMEFIELD.FRAME.ORIGIN.Y + NameField.    BOUNDS.SIZE.HEIGHT+10, 200, 44)];    Phonefield.tag = 101;    Phonefield.delegate = self;    Phonefield.placeholder = @ "Phone";    Phonefield.keyboardtype = Uikeyboardtypedecimalpad;    Phonefield.clearbuttonmode = Uitextfieldviewmodealways; [Self.view ADDsubview:phonefield]; Mailbox input domain Uitextfield *emailfield = [[Uitextfield alloc] Initwithframe:cgrectmake (PHONEFIELD.FRAME.ORIGIN.Y + phoneFi    Eld.bounds.size.height + 10, 200, 44)];    Emailfield.tag = 102;    Emailfield.delegate = self;    Emailfield.placeholder = @ "email";    Emailfield.keyboardtype = uikeyboardtypeemailaddress;    Emailfield.clearbuttonmode = Uitextfieldviewmodealways; [Self.view Addsubview:emailfield];
Uitextfield Hidden Keyboard

1. Click the return of the keyboard to hide the keyboard

This method needs to implement the Uitextfielddelegate protocol in the corresponding. h file file. and add the following method to the. m file

-(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{    [TextField Resignfirstresponder];    return YES;}

2. Click the blank space to hide the keyboard

The implementation of this method is mainly to the current view to increase the Click event, not the Click event to increase the corresponding processing method, here is to hide the keyboard, so we can in the Click event corresponding method to let Uitextfield abandon the first responder.

-(void) dismisskeyboard{    nsarray *subviews = [Self.view subviews];    For (ID inputtext in subviews) {        if ([Inputtext Iskindofclass:[uitextfield class]]) {            if ([Inputtext ISFIRSTRESPO NDEr]) {                [Inputtext resignfirstresponder];}}}    

Add a click event to the current view

UITapGestureRecognizer *dismisskeyboardtap = [[UITapGestureRecognizer alloc]                                                initwithtarget:self                                                 action:@ Selector (Dismisskeyboard)]; [Self.view Addgesturerecognizer:dismisskeyboardtap];
uitextfield--adding checksums for content
-(BOOL) textfieldshouldendediting: (Uitextfield *) textfield{    switch (textfield.tag) {case        100://name        {            NSLog (@ "This is NameField");            Add code to verify the name break            ;        }        Case 101://phone        {            NSLog (@ "The is Phonefield");            Add code to verify the phone break            ;        }        Case 102://email        {            NSLog (@ "The is Emailfield");            Add code to verify email break            ;        }                Default: Break            ;    }    return YES;}

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.