ios - Populate the Selected value in Picker view -


here problem. had uitableview had add , edit functionality. @ first when click on add button displays picker view 3 components. component 1 contains list of projects (select,proj1, proj2, proj3, proj4). select proj3 , click on submit. data able save in data base. here comes problem. saved data able display in tableview list. when click on respective record, navigates detailed edit page see proj4 not selected in picker view. instead selected select following proj1,proj2,proj3 etc,. here code below.

- (void)viewdidload {      [super viewdidload];     self.txtstatus.delegate = self;     [self loaddata];     [self loadprojects];     [self loadtasks];     [self loadsubtasks];     [self benefits];      txthours.enabled = no; }  -(void)benefits {      nsstring *benefitpost =[[nsstring alloc] initwithformat:@"username=%@",[self.projectpicker datasource]];      nsstring *benefiturl = @"http://picker.com/getbenefittypes";     nsurl *url = [nsurl urlwithstring:benefiturl];      nsdata *benefitpostdata = [benefitpost datausingencoding:nsasciistringencoding allowlossyconversion:yes];         nsstring *benefitpostlength = [nsstring stringwithformat:@"%lu", (unsigned long)[benefitpostdata length]];     nsmutableurlrequest *benefitrequest = [[nsmutableurlrequest alloc] init];     [benefitrequest seturl:url];     [benefitrequest sethttpmethod:@"post"];     [benefitrequest setvalue:benefitpostlength forhttpheaderfield:@"content-length"];     [benefitrequest setvalue:@"application/projectpicker" forhttpheaderfield:@"accept"];     [benefitrequest setvalue:@"application/jsonarray" forhttpheaderfield:@"content-type"];     [benefitrequest sethttpbody:benefitpostdata];       nserror *error = [[nserror alloc] init];     nshttpurlresponse *response = nil;     nsdata *benefiturldata=[nsurlconnection sendsynchronousrequest:benefitrequest returningresponse:&response error:&error];     nsurlrequest *benefiturlrequest = [nsurlrequest requestwithurl:url                                                        cachepolicy:nsurlrequestreturncachedataelseload                                                    timeoutinterval:30];       // make synchronous request     benefiturldata = [nsurlconnection sendsynchronousrequest:benefiturlrequest                                            returningresponse:&response                                                        error:&error];     if ([response statuscode] >= 200 && [response statuscode] < 300)     {         nsstring *benefitresponsedata = [nsjsonserialization jsonobjectwithdata:benefiturldata                                                                         options:nsjsonreadingallowfragments error:&error];          nsarray *benefitentries = [nsjsonserialization jsonobjectwithdata:[benefitresponsedata datausingencoding:nsutf8stringencoding]                                                                   options:0 error:&error];         if(!benefitentries)         {             nslog(@"error : %@", error);         }         else{              (nsdictionary *benefitentry in benefitentries) {                 benid = [benefitentries valueforkey:@"id_leaves"];                 benefitnames = [benefitentries valueforkey:@"nm_leaves"];              }             nslog(@"benefitnames : %@", benefitnames);             _projectpicker.delegate = self;             _projectpicker.datasource = self;             [self loadprojects];         }      }       else {      } } -(void)loadprojects {     nsstring *post =[[nsstring alloc] initwithformat:@"username=%@",[self.projectpicker datasource]];     //nsstring *pickername = [self.projectpicker datasource];     //nsstring *key = @"da9s^a2rp4na6r$ikiasav3is@niwsa";      //nsstring *encrypteduname = [aescrypt encrypt:pickername password:key];      // code project loading     nsstring *projecturltemp = @"http://picker.com/getassignedprojects";     nsstring *str = [[nsuserdefaults standarduserdefaults] valueforkey:@"userloginidsession"];     nsstring *usrid = str;     nsstring * projecturl =[nsstring stringwithformat:@"%@/%@",projecturltemp,usrid];       nsurl *url = [nsurl urlwithstring:projecturl];      nsdata *postdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:yes];         nsstring *postlength = [nsstring stringwithformat:@"%lu", (unsigned long)[postdata length]];     nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init];     [request seturl:url];     [request sethttpmethod:@"post"];     [request setvalue:postlength forhttpheaderfield:@"content-length"];     [request setvalue:@"application/projectpicker" forhttpheaderfield:@"accept"];     [request setvalue:@"application/jsonarray" forhttpheaderfield:@"content-type"];     [request sethttpbody:postdata];       nserror *error = [[nserror alloc] init];     nshttpurlresponse *response = nil;     nsdata *urldata=[nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error];     nsurlrequest *urlrequest = [nsurlrequest requestwithurl:url                                                 cachepolicy:nsurlrequestreturncachedataelseload                                             timeoutinterval:30];       // make synchronous request     urldata = [nsurlconnection sendsynchronousrequest:urlrequest                                     returningresponse:&response                                                 error:&error];     if ([response statuscode] >= 200 && [response statuscode] < 300)     {         nsstring *responsedata = [nsjsonserialization jsonobjectwithdata:urldata                                                                  options:nsjsonreadingallowfragments error:&error];          nsarray *entries = [nsjsonserialization jsonobjectwithdata:[responsedata datausingencoding:nsutf8stringencoding]                                                            options:0 error:&error];         if(!entries)         {             nslog(@"error : %@", error);         }         else{              (nsdictionary *entry in entries) {                 projid = [entries valueforkey:@"id_project"];                 projectnames = [entries valueforkey:@"nm_project"];             }             randomselection=[benefitnames arraybyaddingobjectsfromarray:projectnames];             randomselectionid = [benid arraybyaddingobjectsfromarray:projid];             //nslog(@"error : %@", projectnames);             //nslog(@"projid : %@", projid);             _projectpicker.delegate = self;             _projectpicker.datasource = self;         }      }       else {      } } -(void)loadtasks {     // code tasks loading      nsstring *post =[[nsstring alloc] initwithformat:@"username=%@",[self.projectpicker datasource]];     nsstring *nstaskurllocal = @"http://picker.com/getassignedtasks/";     nsstring *str = [[nsuserdefaults standarduserdefaults] valueforkey:@"userloginidsession"];     nsstring *usrid = str;      nsstring * productidstring =[nsstring stringwithformat:@"%@/%@",lblprojects.text,usrid];     //nslog(@"astring : %@", productidstring);     nsstring *astring = [nstaskurllocal stringbyappendingstring:productidstring];       nsurl *nstaskurl = [nsurl urlwithstring:astring];     //nslog(@"nstaskurl : %@", nstaskurl);      nsdata *nstaskpostdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:yes];      nsstring *nstaskpostlength = [nsstring stringwithformat:@"%lu", (unsigned long)[nstaskpostdata length]];     nsmutableurlrequest *nstaskrequest = [[nsmutableurlrequest alloc] init];     [nstaskrequest seturl:nstaskurl];     [nstaskrequest sethttpmethod:@"post"];     [nstaskrequest setvalue:nstaskpostlength forhttpheaderfield:@"content-length"];     [nstaskrequest setvalue:@"application/projectpicker" forhttpheaderfield:@"accept"];     [nstaskrequest setvalue:@"application/jsonarray" forhttpheaderfield:@"content-type"];     [nstaskrequest sethttpbody:nstaskpostdata];       nserror *nstaskerror = [[nserror alloc] init];     nshttpurlresponse *nstaskresponse = nil;     nsdata *nstaskurldata=[nsurlconnection sendsynchronousrequest:nstaskrequest returningresponse:&nstaskresponse error:&nstaskerror];     nsurlrequest *nstaskurlrequest = [nsurlrequest requestwithurl:nstaskurl                                                       cachepolicy:nsurlrequestreturncachedataelseload                                                   timeoutinterval:30];       // make synchronous request     nstaskurldata = [nsurlconnection sendsynchronousrequest:nstaskurlrequest                                           returningresponse:&nstaskresponse                                                       error:&nstaskerror];     if ([nstaskresponse statuscode] >= 200 && [nstaskresponse statuscode] < 300)     {         nsstring *nstaskresponsedata = [nsjsonserialization jsonobjectwithdata:nstaskurldata                                                                        options:nsjsonreadingallowfragments error:&nstaskerror];          nsarray *nstaskentries = [nsjsonserialization jsonobjectwithdata:[nstaskresponsedata datausingencoding:nsutf8stringencoding]                                                                  options:0 error:&nstaskerror];         if(!nstaskentries)         {             nslog(@"error : %@", nstaskerror);         }         else{              (nsdictionary *nstaskentry in nstaskentries) {                 taskid = [nstaskentries valueforkey:@"id_task"];                 tasknames = [nstaskentries valueforkey:@"task_name"];                  //nslog(@"error : %@", tasknames); //log see result in console // kiran             }              _projectpicker.delegate = self;             _projectpicker.datasource = self;         }      }       else {      } } -(void)loadsubtasks {     // code sub tasks loading     nsstring *post =[[nsstring alloc] initwithformat:@"username=%@",[self.projectpicker datasource]];     nsstring *nssubtaskurllocal = @"http://picker.com/getassignedsubtasks/";     nsstring *str = [[nsuserdefaults standarduserdefaults] valueforkey:@"userloginidsession"];     nsstring *usrid = str;     nsstring * subproductidstring =[nsstring stringwithformat:@"%@/%@",lbltasks.text,usrid];     //nslog(@"asubstring : %@", subproductidstring);     nsstring *astring = [nssubtaskurllocal stringbyappendingstring:subproductidstring];       nsurl *nssubtaskurl = [nsurl urlwithstring:astring];     //nslog(@"nsubstaskurl : %@", nssubtaskurl);      nsdata *nssubtaskpostdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:yes];      nsstring *nssubtaskpostlength = [nsstring stringwithformat:@"%lu", (unsigned long)[nssubtaskpostdata length]];     nsmutableurlrequest *nssubtaskrequest = [[nsmutableurlrequest alloc] init];     [nssubtaskrequest seturl:nssubtaskurl];     [nssubtaskrequest sethttpmethod:@"post"];     [nssubtaskrequest setvalue:nssubtaskpostlength forhttpheaderfield:@"content-length"];     [nssubtaskrequest setvalue:@"application/projectpicker" forhttpheaderfield:@"accept"];     [nssubtaskrequest setvalue:@"application/jsonarray" forhttpheaderfield:@"content-type"];     [nssubtaskrequest sethttpbody:nssubtaskpostdata];       nserror *nssubtaskerror = [[nserror alloc] init];     nshttpurlresponse *nssubtaskresponse = nil;     nsdata *nssubtaskurldata=[nsurlconnection sendsynchronousrequest:nssubtaskrequest returningresponse:&nssubtaskresponse error:&nssubtaskerror];     nsurlrequest *nssubtaskurlrequest = [nsurlrequest requestwithurl:nssubtaskurl                                                          cachepolicy:nsurlrequestreturncachedataelseload                                                      timeoutinterval:30];       // make synchronous request     nssubtaskurldata = [nsurlconnection sendsynchronousrequest:nssubtaskurlrequest                                              returningresponse:&nssubtaskresponse                                                          error:&nssubtaskerror];     if ([nssubtaskresponse statuscode] >= 200 && [nssubtaskresponse statuscode] < 300)     {         nsstring *nssubtaskresponsedata = [nsjsonserialization jsonobjectwithdata:nssubtaskurldata                                                                           options:nsjsonreadingallowfragments error:&nssubtaskerror];          nsarray *nssubtaskentries = [nsjsonserialization jsonobjectwithdata:[nssubtaskresponsedata datausingencoding:nsutf8stringencoding]                                                                     options:0 error:&nssubtaskerror];         if(!nssubtaskentries)         {             nslog(@"error : %@", nssubtaskentries);         }         else{              (nsdictionary *nssubtaskentry in nssubtaskentries) {                 subtskid = [nssubtaskentries valueforkey:@"id_sub_task"];                 subtasknames = [nssubtaskentries valueforkey:@"subtask_name"];                  //nslog(@"error : %@", subtasknames); //log see result in console // kiran             }              _projectpicker.delegate = self;             _projectpicker.datasource = self;         }      }       else {      } }  -(void)loaddata {     nsstring *eventdate = self.projidstocancel;     [[nsuserdefaults standarduserdefaults] setobject:eventdate forkey:@"eventdate"];     [[nsuserdefaults standarduserdefaults] synchronize];     nsstring *post =[[nsstring alloc] initwithformat:@"username=%@",[self.projectpicker datasource]];     //nsstring *pickername = [self.projectpicker datasource];     //nsstring *key = @"da9s^a2rp4na6r$ikiasav3is@niwsa";      //nsstring *encrypteduname = [aescrypt encrypt:pickername password:key];      // code project loading     nsstring *projecturltemp = @"http://picker.com/getdetailsbyid";     nsstring *str = [[nsuserdefaults standarduserdefaults] valueforkey:@"userloginidsession"];     nsstring *usrid = str;     nsstring * projecturl =[nsstring stringwithformat:@"%@/%@",projecturltemp,self.hdnrowcount];     nsurl *url = [nsurl urlwithstring:projecturl];      nslog(@"projecturl : %@", projecturl);     nsdata *postdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:yes];         nsstring *postlength = [nsstring stringwithformat:@"%lu", (unsigned long)[postdata length]];     nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init];     [request seturl:url];     [request sethttpmethod:@"post"];     [request setvalue:postlength forhttpheaderfield:@"content-length"];     [request setvalue:@"application/projectpicker" forhttpheaderfield:@"accept"];     [request setvalue:@"application/jsonarray" forhttpheaderfield:@"content-type"];     [request sethttpbody:postdata];       nserror *error = [[nserror alloc] init];     nshttpurlresponse *response = nil;     nsdata *urldata=[nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error];     nsurlrequest *urlrequest = [nsurlrequest requestwithurl:url                                                 cachepolicy:nsurlrequestreturncachedataelseload                                             timeoutinterval:30];       // make synchronous request     urldata = [nsurlconnection sendsynchronousrequest:urlrequest                                     returningresponse:&response                                                 error:&error];     if ([response statuscode] >= 200 && [response statuscode] < 300)     {         nsstring *responsedata = [nsjsonserialization jsonobjectwithdata:urldata                                                                  options:nsjsonreadingallowfragments error:&error];          nsarray *entries = [nsjsonserialization jsonobjectwithdata:[responsedata datausingencoding:nsutf8stringencoding]                                                            options:0 error:&error];          if(!entries)         {             nslog(@"error : %@", error);         }         else{              (nsdictionary *entry in entries) {                 projectnames = [entries valueforkey:@"nm_project"];                 tasknames = [entries valueforkey:@"task_name"];                 subtasknames = [entries valueforkey:@"subtask_name"];                 hdnlblprojects.text = [[entries valueforkey:@"nm_project"]componentsjoinedbystring:@""];                 hdnlbltasks.text = [[entries valueforkey:@"task_name"]componentsjoinedbystring:@""];                 hdnlblsubtasks.text = [[entries valueforkey:@"subtask_name"]componentsjoinedbystring:@""];                 txthours.text = [[entries valueforkey:@"no_hours"]componentsjoinedbystring:@""];                 txtstatus.text = [[entries valueforkey:@"status"]componentsjoinedbystring:@""];             }             hrsdiff1 = [txthours.text floatvalue];             //nslog(@"tasknames : %@", tasknames);             //nslog(@"subtask : %@", subtasknames);         }      }       else {      } } -(nsinteger)numberofcomponentsinpickerview:(uipickerview *)pickerview {     return 3; } -(nsinteger)pickerview:(uipickerview *)pickerview numberofrowsincomponent:(nsinteger)component {     int numberofrows = 0;     switch (component) {          case 0:             numberofrows = [randomselection count];             [pickerview reloadcomponent:1];             break;         case 1:             numberofrows = [tasknames count];             [pickerview reloadcomponent:2];             break;         case 2:             numberofrows = [subtasknames count];             break;     }     return numberofrows; } -(nsstring *)pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component {     nsstring *title;     if(component == 0) {         [pickerview reloadcomponent:1];         [pickerview reloadcomponent:2];         title = [randomselection objectatindex:row];     }     else if (component == 1){         [pickerview reloadcomponent:2];         title = [tasknames objectatindex:row];     }     else{         title = [subtasknames objectatindex:row];      }     return title;  }  -(void)pickerview:(uipickerview *)pickerview didselectrow:(nsinteger)row incomponent:(nsinteger)component {     //nslog(@"%@",myarraystring);     //nslog(@"%@",mytaskarraystring);     if(component == 0){         nsnumber *myprojectarraystring = [randomselectionid objectatindex:row];         lblprojects.text = [nsstring stringwithformat:@"%@",myprojectarraystring];         lblprojects.hidden = yes;         hdnlblprojects.text = [randomselection objectatindex:[pickerview selectedrowincomponent:0]];          [self loadtasks];         [pickerview reloadcomponent:1];         [pickerview reloadcomponent:2];     }     //lblprojects.hidden = yes;     else if(component == 1)     {         nsnumber *mytaskarraystring = [taskid objectatindex:row];         lbltasks.text = [nsstring stringwithformat:@"%@",mytaskarraystring];         lbltasks.hidden = yes;         hdnlbltasks.text = [tasknames objectatindex:[pickerview selectedrowincomponent:1]];         [self loadsubtasks];         [pickerview reloadcomponent:2];     }     else if(component == 2)     {         nsnumber *mysubtaskarraystring = [subtskid objectatindex:row];         lblsubtasks.text = [nsstring stringwithformat:@"%@",mysubtaskarraystring];         nslog(@"%@",lblsubtasks.text);         lblsubtasks.hidden = yes;         hdnlblsubtasks.text = [subtasknames objectatindex:[pickerview selectedrowincomponent:2]];          //lbltasks.text = [tasknames objectatindex:[pickerview selectedrowincomponent:1]];         //lbltasks.text = [nsstring stringwithformat:@"%@", mytaskarraystring];         //lblsubtasks.text = [subtasknames objectatindex:[pickerview selectedrowincomponent:2]];     } } 


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -