Populate the Selected value in Picker view

2019-08-28 06:14发布

问题:

Here is my Problem. I had a uitableview which had add and edit functionality. At first when I click on Add button it displays a picker view with three components. Component 1 contains list of projects (Select,proj1, proj2, proj3, proj4). I select proj3 and click on submit. Data is able to save in the data base. Now Here comes my problem. The saved data is able to display in the tableview list. When I click on the respective record, it navigates to the detailed edit page where I see proj4 is not selected in the picker view. Instead it is selected as Select following proj1,proj2,proj3 etc,. Here is the 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{

            for (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 for 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{

            for (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 for 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{

            for (NSDictionary *nstaskentry in nstaskentries) {
                taskID = [nstaskentries valueForKey:@"ID_TASK"];
                taskNames = [nstaskentries valueForKey:@"TASk_NAME"];

                //NSLog(@"Error : %@", taskNames); //log to see the result in console // by Kiran
            }

            _projectpicker.delegate = self;
            _projectpicker.dataSource = self;
        }

    }       else {

    }
}
-(void)loadsubtasks
{
    // Code for 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{

            for (NSDictionary *nssubtaskentry in nssubtaskentries) {
                subtskID = [nssubtaskentries valueForKey:@"ID_SUB_TASK"];
                subtaskNames = [nssubtaskentries valueForKey:@"SUBTASK_NAME"];

                //NSLog(@"Error : %@", subtaskNames); //log to see the result in console // by 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 for 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{

            for (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]];
    }
}