OStack程序员社区-中国程序员成长平台

标题: ios - 在 uipickerview 中显示选中的值 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 21:11
标题: ios - 在 uipickerview 中显示选中的值

我的详细页面中有一个 uipickerview。当我编辑来 self 的 tableviewcell 的相应记录时,我想获得默认选择的值。简而言之,我有一个包含 10 条记录的表格 View 。当我从 tableview 单元格中选择一条记录时,它会导航到详细页面,该页面包含三个选择器 View ,每个 View 都有一个组件。我希望选择器 View 默认加载选定的值。下面是我如何加载数据的代码。如何在 -(NSString *)pickerViewUIPickerView *)pickerView titleForRowNSInteger)row 中显示选定的值,还有一件事是我正在为这个页面使用 UINavigationcontroller?小伙伴们可以帮帮我吗

-(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://xxx.xxx/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"OST"];
    [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];
            _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 : %@&assword :%@",[self.projectpicker dataSource],[self.projectpicker delegate]];
    //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://xxx.xxxx/GetDetailsByID";
    NSString *str = [[NSUserDefaults standardUserDefaults] valueForKey:@"UserLoginIdSession"];
    NSString *usrid = str;
    NSString * projecturl =[NSString stringWithFormat:@"%@/%@",projecturltemp,self.hdnRowcount];
    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:@"OST"];
    [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:@""];
                lblBenefitsLeaves.text = [[entries valueForKey:@"NM_LEAVE"]componentsJoinedByString:@""];
                BenefitNames = [entries valueForKey:@"NM_LEAVE"];
                projID = [entries valueForKey:@"ID_PROJECT"];
                taskID = [entries valueForKey:@"ID_TASK"];
                subtskID = [entries valueForKey:@"ID_SUB_TASK"];
            }
            lblProjects.text = [[projID valueForKey:@"description"] componentsJoinedByString:@""];
            taskstring = [[taskID valueForKey:@"description"] componentsJoinedByString:@""];
            subtaskstring = [[subtskID valueForKey:@"description"] componentsJoinedByString:@""];
            bentest = [[BenefitNames valueForKey:@"description"] componentsJoinedByString:@""];
            hrsdiff1 = [txthours.text floatValue];
        }

    }       else {

    }
}

-(NSInteger)numberOfComponentsInPickerViewUIPickerView *)pickerView
{
    return 1;
}
-(NSInteger)pickerViewUIPickerView *)pickerView numberOfRowsInComponentNSInteger)component
{
    int numberofRows = 0;
    if([pickerView isEqual: _projectpicker]){
        numberofRows = [randomSelection count];
        return numberofRows;
    }
    else if([pickerView isEqual: _taskspicker]){
        numberofRows = [taskNames count];
        return numberofRows;
    }
    else if([pickerView isEqual: _subtaskspicker]){
        numberofRows = [subtaskNames count];
        return numberofRows;
    }
}
-(NSString *)pickerViewUIPickerView *)pickerView titleForRowNSInteger)row forComponentNSInteger)component
{
    NSString *title = @"Test";
    if([pickerView isEqual: _projectpicker]){
        title = [randomSelection objectAtIndex:row];
        return title;
    }

    else if ([pickerView isEqual: _taskspicker]){
        title = [taskNames objectAtIndex:row];
        return title;
    }
    else if ([pickerView isEqual: _subtaskspicker]){
        title = [subtaskNames objectAtIndex:row];
        return title;
    }

}

-(void)pickerViewUIPickerView *)pickerView didSelectRowNSInteger)row inComponent:(NSInteger)component
{
    //NSLog(@"%@",myArrayString);
    //NSLog(@"%@",myTaskArrayString);
   if([pickerView isEqual: _projectpicker]){
        NSNumber *myProjectArrayString = [randomSelectionID objectAtIndex:row];
        lblProjects.text = [NSString stringWithFormat:@"%@",myProjectArrayString];
        lblProjects.hidden = YES;
        lblBenefitsLeaves.text = [randomSelection objectAtIndex:row];
        hdnlblProjects.text = [randomSelection objectAtIndex:row];
        rownoedit = row;
       lblTasks.text = @"0";
       hdnlblTasks.text = @"--Select--";
       lblSubTasks.text = @"0";
       hdnlblSubTasks.text = @"--Select--";
        [self loadtasks];

    }
    //lblProjects.hidden = YES;
    else if([pickerView isEqual: _taskspicker])
    {
        NSNumber *myTaskArrayString = [taskID objectAtIndex:row];
        lblTasks.text = [NSString stringWithFormat:@"%@",myTaskArrayString];
        lblTasks.hidden = YES;
        hdnlblTasks.text = [taskNames objectAtIndex:row];
        lblSubTasks.text = @"0";
        hdnlblSubTasks.text = @"--Select--";
        [self loadsubtasks];

    }
    else if([pickerView isEqual: _subtaskspicker])
    {
        NSNumber *mysubtaskArrayString = [subtskID objectAtIndex:row];
        lblSubTasks.text = [NSString stringWithFormat:@"%@",mysubtaskArrayString];
        lblSubTasks.hidden = YES;
        hdnlblSubTasks.text = [subtaskNames objectAtIndex:row];

        //lblTasks.text = [taskNames objectAtIndex:[pickerView selectedRowInComponent:1]];
        //lblTasks.text = [NSString stringWithFormat:@"%@", myTaskArrayString];
        //lblSubTasks.text = [subtaskNames objectAtIndex:[pickerView selectedRowInComponent:2]];
    }
}



Best Answer-推荐答案


首先定义一个静态变量,并在加载时为其赋予初始值

ex : static int selectedRow = 3

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
selectedRow = [pickerView selectedRowInComponent:0];

}

然后在你显示pickerView的地方放置这一行

  [self selectRow:selectedRow inComponent:0 animated:YES];

关于ios - 在 uipickerview 中显示选中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22832712/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4