gpt4 book ai didi

ios - 在uipickerview中显示选中的值

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:14 25 4
gpt4 key购买 nike

我的详细页面有一个uipickerview。当我编辑来 self 的 tableviewcell 的相应记录时,我想获得默认选择的值。简而言之,我有一个包含 10 条记录的 TableView 。当我从 tableview 单元格中选择一条记录时,它会导航到详细页面,其中包含三个选择器 View ,每个 View 都有一个组件。我希望选择器 View 默认加载所选值。下面是我如何加载数据的代码。如何在 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)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:@"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];
_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 : %@&Password :%@",[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:@"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:@""];
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)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)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 *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)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)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)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]];
}
}

最佳答案

首先定义一个静态变量并给它你喜欢的初始值加载

例如: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com