062 set a clear button in the text input box

Source: Internet
Author: User

The effect is as follows:

ViewController.h

1 #import <UIKit/UIKit.h>23@interface Viewcontroller:uiviewcontroller <UITextFieldDelegate>4 @property (Strong, Nonatomic) Nsmutablearray *Marrtextfield; 5 6 @end

Viewcontroller.m

1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4- (void) Layoutui;5-(Uitextfield *) Newtextfield: (Uitextfieldviewmode) Clearbuttonmode frame: (cgrect) rect;6- (void) Hidekeyboard: (ID) sender;7 @end8 9 @implementationViewcontrollerTen- (void) Viewdidload { One [Super Viewdidload]; A      - [self layoutui]; -      theUITapGestureRecognizer *tapgesturerecognizer =[[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Hidekeyboard:)]; - [Self.view Addgesturerecognizer:tapgesturerecognizer]; - } -  +- (void) didreceivememorywarning { - [Super didreceivememorywarning]; +     //Dispose of any resources the can be recreated. A } at  - #pragmaMark-private Methods - /** - * Hide Keyboard -  * - * @param sender Event Trigger in  */ -- (void) Hidekeyboard: (ID) Sender { toNSLog (@"Tap Gesture recognizer, remove some controls as the first responder"); +      for(Uitextfield *TXTFinch_marrtextfield) { - [TXTF Resignfirstresponder]; the     } * } $ Panax Notoginseng- (void) Layoutui { -_marrtextfield = [[Nsmutablearray alloc] Initwithcapacity:4]; the      +CGRect recttemp = CGRectMake ( -, +, the, +); AUitextfield *txtftemp =[self newtextfield:uitextfieldviewmodenever frame:recttemp]; the [Self.view addsubview:txtftemp]; + [_marrtextfield addobject:txtftemp]; -      $RECTTEMP.ORIGIN.Y + = -; $Txtftemp =[self newtextfield:uitextfieldviewmodewhileediting frame:recttemp]; - [Self.view addsubview:txtftemp]; - [_marrtextfield addobject:txtftemp]; the      -RECTTEMP.ORIGIN.Y + = -;WuyiTxtftemp =[self newtextfield:uitextfieldviewmodeunlessediting frame:recttemp]; the [Self.view addsubview:txtftemp]; - [_marrtextfield addobject:txtftemp]; Wu      -RECTTEMP.ORIGIN.Y + = -; AboutTxtftemp =[self newtextfield:uitextfieldviewmodealways frame:recttemp]; $ [Self.view addsubview:txtftemp]; - [_marrtextfield addobject:txtftemp]; - } -  A-(Uitextfield *) Newtextfield: (Uitextfieldviewmode) Clearbuttonmode frame: (cgrect) rect { +Uitextfield *txtfmessage =[[Uitextfield alloc] initwithframe:rect]; theTxtfmessage.textalignment =Nstextalignmentleft; -Txtfmessage.textcolor = [Uicolor colorwithred:0.433Green0.829Blue1.000Alpha1.000]; $Txtfmessage.borderstyle =Uitextborderstyleroundedrect; the      theTxtfmessage.Delegate=Self ; theTxtfmessage.clearbuttonmode =Clearbuttonmode; the     Switch(clearbuttonmode) { -          CaseUitextfieldviewmodenever: inTxtfmessage.text =@"Uitextfieldviewmodenever";//do not show empty button; default value the              Break; the          Caseuitextfieldviewmodewhileediting: AboutTxtfmessage.text =@"uitextfieldviewmodewhileediting";//text content is not empty, and the empty button is displayed when editing state (that is, getting focus) the              Break; the          Caseuitextfieldviewmodeunlessediting: theTxtfmessage.text =@"uitextfieldviewmodeunlessediting";//The empty button appears when the text content is not empty and is not in the edit state (that is, it loses focus) +              Break; -          Caseuitextfieldviewmodealways: theTxtfmessage.text =@"uitextfieldviewmodealways";//when text content is not empty, get focus and lose focus show empty buttonBayi              Break; the     } the     returnTxtfmessage; - } -  the #pragmaMark-textfield the-(BOOL) Textfieldshouldclear: (Uitextfield *) TextField { theNSLog (@"textfieldshouldclear:%@", Textfield.text);//The Record emptying button triggers the emptying of the text content; For example: Uitextfieldviewmodewhileediting the     returnYES; - } the  the @end

062 set a clear button in the text input box

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.