选择组件1选择器后,将数据加载到组件2中查看IOS [英] Loading data in Component 2 after selecting component 1 picker View IOS
问题描述
选择项目时,在我的pickerview中有三个组件(项目,任务和子任务),我能够获得项目名称和相应的项目ID(项目ID在标签中).我的要求是我想将项目ID发送到NSURL,以便我可以加载分配给该项目ID的各个任务.这是我的以下代码. ViewDidLoad:
There are Three components (projects, tasks and sub tasks )in my pickerview when I select project, I was able to get the project name and respective project ID (Project ID is in label). My requirement is I want to send the project ID to NSURL so that I can load the respective tasks that are assigned to that project ID. Here is my Below Code. ViewDidLoad:
// Code for Tasks loading
NSString *nsTaskurllocal = @"http://test.com/";
NSString *usrid = @"313";
NSString * productIdString =[NSString stringWithFormat:@"%@/%@",[self.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)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSNumber *myProjectArrayString = [projID objectAtIndex:row];
//NSNumber *myTaskArrayString = [taskID objectAtIndex:row];
//NSLog(@"%@",myArrayString);
//NSLog(@"%@",myTaskArrayString);
lblProjects.text = [NSString stringWithFormat:@"%@",myProjectArrayString];
//lblProjects.hidden = YES;
lblTasks.text = [taskNames objectAtIndex:[pickerView selectedRowInComponent:1]];
//lblTasks.text = [NSString stringWithFormat:@"%@", myTaskArrayString];
lblSubTasks.text = [subtaskNames objectAtIndex:[pickerView selectedRowInComponent:2]];
}
预先感谢 基兰·库玛(Kiran Kumar)
Thanks in Advance Kiran Kumar
推荐答案
您需要区分总计数据和显示的数据.
You need to differentiate between your total data, and your displayed data.
因此,下载所有数据并将其保存在projectNames
,taskNames
和subtaskNames
中.但还有2个其他属性:currentTaskNames
和currentSubtaskNames
(您不需要
cProjectNames
,因为用户始终可以看到所有项目名称.
So, download all of your data and save it in projectNames
, taskNames
and subtaskNames
. But also have 2 other properties: currentTaskNames
and currentSubtaskNames
(you don't need
cProjectNames
because the user can always see all project names.
下载后:
self.currentTaskNames = taskNames;
self.currentSubtaskNames = subtaskNames;
现在,当用户选择一个项目时,过滤可用的任务和子任务.
Now, when the user selects a project, filter the tasks and subtasks that are available.
switch (component) {
case 0:
{
NSString *project = [projectNames objectAtIndex:row];
self.currentTaskNames = [taskNames filteredArrayUsingPredicate:...];
break;
}
case 1:
{
NSString *task = [taskNames objectAtIndex:row];
self.currentSubtaskNames = [subtaskNames filteredArrayUsingPredicate:...];
break;
}
case 2:
// do something interesting
break;
}
您需要填写谓词,以根据所选项目和任务过滤出不合适的任务和子任务.
You need to fill in the predicates which filter out the tasks and subtasks that aren't appropriate based on the selected project and task.
也请停止使用标签进行数据存储...
Also, stop using labels for data storage...
这篇关于选择组件1选择器后,将数据加载到组件2中查看IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!