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