【发布时间】:2016-07-26 03:18:03
【问题描述】:
我是 IOS 新手,正在尝试解决这个问题。
我正在尝试将 json 数据从一个视图控制器传递到另一个(TableViewcontroller),我希望在 prepareForSegue 方法(然后传递我从按钮获得的 json 数据)之前执行按钮操作(这具有 json 数据)动作)到 TableViewcontroller。
但是我的 preparesegue 方法是在按钮操作和 tableview 没有数据之前先执行的?
但是当我第二次单击该按钮时,我得到了 tableview json 数据。
如果我使用直接 url,当我尝试将 Textfield 传递给 URl 时,就会发生这种情况。
有人可以帮我解决这个问题。 如果还有其他方法可以做到这一点,请告诉我。
这是第一个VC中的代码:
- (IBAction)buttonShow:(id)sender {
NSURLSession *session = [NSURLSession sharedSession];
NSString *urlString = [NSString stringWithFormat:@"Some API URL=%@",self.ingredientTextfield.text];
NSURL *url = [[NSURL alloc]initWithString:urlString];
req = [[NSMutableURLRequest alloc]initWithURL:url];
NSURLSessionDataTask *datatask = [session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *rcCount = [json objectForKey:@"count"];
NSLog(@"Recipe Count : %@",rcCount);
NSMutableDictionary *recipedata = [json objectForKey:@"recipes"];
NSLog(@"Recipe data : %@",recipedata);
NSMutableArray *titlearray = [[NSMutableArray alloc]init];
for (NSMutableDictionary *singlerecipedata in recipedata) {
NSString *title = [singlerecipedata objectForKey:@"title"];
NSLog(@"Recipe title : %@",title);
[titlearray addObject:title];
NSLog(@"tittlearray : %@",titlearray);
}
actualDataArray = [[NSArray alloc]initWithArray:titlearray];
NSLog(@"tittle array inside for:%@",actualDataArray);
}];
NSLog(@"tittle array outside :%@",actualDataArray);
[datatask resume];
[self performSegueWithIdentifier:@"segueIdentifier" sender:sender];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"segueIdentifier"]) {
RecipesTableViewController *new = [segue destinationViewController];
NSLog(@"in segue :%@",actualDataArray);
[new setMyrecipeTittleArray:actualDataArray];
}
}
【问题讨论】:
-
你能提供一些代码吗?
标签: ios json uitableview segue viewcontroller