ios - How to use date picker instead of textfield? -
i have 2 textfields , 1 button. used textfield entered date must use date picker.
this code entering date textfield , display fields labels.how change date picker code?
viewcontroller.h
@interface viewcontroller : uiviewcontroller <nsxmlparserdelegate> - (ibaction)send:(uibutton *)sender; @property (unsafe_unretained, nonatomic) iboutlet uitextfield *date1; @property (unsafe_unretained, nonatomic) iboutlet uitextfield *date2; //will here define date picker @end viewcontroller.m
@interface viewcontroller (){ nsmutabledata *webdata; nsxmlparser *xmlparser; nsmutablestring *returnsoap; bool treturn; } @end @implementation viewcontroller @synthesize date1,date2; - (ibaction)send:(uibutton *)sender{ nsstring *msgsoap= [nsstring stringwithformat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" "<soap:envelope xmlns:xsi=\"http://www.w3.org/2001/xmlschema- instance\" xmlns:xsd=\"http://www.w3.org/2001/xmlschema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" "<soap:body>\n" "<showdetails xmlns=\"http://tempuri.org/\">\n" "<date1>%@</date1>\n" "<date2>%@</date2>\n" "</showdetails>\n" "</soap:body>\n" "</soap:envelope>\n",date1.text,date2.text]; nslog(@"soap resulr = \n%@\n\n", msgsoap); nsurl *url = [nsurl urlwithstring:@"http://webservice/service.asmx"]; nsmutableurlrequest *therequest = [nsmutableurlrequest requestwithurl:url]; nsstring *dnmsg = [nsstring stringwithformat:@"%d", [msgsoap length]]; [therequest addvalue:@"text/xml; charset=utf-8" forhttpheaderfield:@"content-type"]; [therequest addvalue: @"http://tempuri.org/showdetails" forhttpheaderfield:@"soapaction"]; [therequest dnmsg forhttpheaderfield:@"content-length"]; [therequest sethttpmethod:@"post"]; [therequest sethttpbody:[msgsoap datausingencoding:nsutf8stringencoding]]; nsurlconnection *conn = [[nsurlconnection alloc] initwithrequest:therequest delegate:self]; if(con){ webdata = [nsmutabledata data]; }else{ nslog(@"connection error."); } } thank
if i'm getting correct want use datepicker user enter correct date , don't have validate entered date.
you can use inputview of textfield. use inputaccessoryview dispaly view can put buttons dismiss inputview.
@interface viewcontroller () { uidatepicker *_datepicker; uitoolbar *_pickertoolbar; uitextfield *_activetextfield; nsdateformatter *_dateformatter; } @end @implementation viewcontroller - (void)addinputviewtotextfield:(uitextfield *)textfield{ if (!_datepicker) { _datepicker = [[uidatepicker alloc]init]; [_datepicker setdatepickermode:uidatepickermodedate]; [_datepicker setdate:[nsdate date]]; } textfield.inputview = _datepicker; if (!_pickertoolbar) { _pickertoolbar =[[uitoolbar alloc]initwithframe:cgrectmake(0,0, self.view.frame.size.width,44)]; _pickertoolbar.barstyle =uibarstyleblackopaque; uibarbuttonitem *cancelbutton = [[uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemcancel target:self action:@selector(cancelbuttonpressed:)]; uibarbuttonitem *flexiblespace =[[uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace target:self action:nil]; uibarbuttonitem *donebutton =[[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemdone target:self action:@selector(donebuttonpressed:)]; [_pickertoolbar setitems:@[cancelbutton,flexiblespace, donebutton]]; } textfield.inputaccessoryview = _pickertoolbar; } - (void)donebuttonpressed:(id)sender{ if (!_dateformatter) { _dateformatter = [nsdateformatter new]; [_dateformatter setdateformat:@"dd.mm.yyyy"]; } _activetextfield.text = [_dateformatter stringfromdate:_datepicker.date]; [_activetextfield resignfirstresponder]; } - (void)cancelbuttonpressed:(id)sender{ [_activetextfield resignfirstresponder]; } - (void)textfielddidbeginediting:(uitextfield *)textfield{ _activetextfield = textfield; [self addinputviewtotextfield:textfield]; } - (void)textfielddidendediting:(uitextfield *)textfield{ _activetextfield = nil; }
Comments
Post a Comment