我是 IOS 新手,正在尝试解决这个问题。
我正在尝试将json数据从一个 View Controller 传递到另一个 View Controller (TableViewcontroller),并且我希望在prepareForSegue方法(然后传递我从按钮获得的json数据)之前执行按钮操作(它具有json数据) Action )到 TableView Controller 。
但是我的preparesegue方法是在按钮操作之前首先执行的,并且tableview没有获取数据?
但是当我第二次单击该按钮时,我得到了 tableview json 数据。
当我尝试将 Textfield 传递给 URl 时会发生这种情况,如果我使用直接 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];
}
}
最佳答案
在您的 For 循环之间放置 Prepare for Segue,然后使用断点您可以检查结果!
您的代码必须是:
- (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];
[self performSegueWithIdentifier:@"segueIdentifier" sender:sender];
NSLog(@"tittle array inside for:%@",actualDataArray);
}];
NSLog(@"tittle array outside :%@",actualDataArray);
[datatask resume];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"segueIdentifier"]) {
RecipesTableViewController *new = [segue destinationViewController];
NSLog(@"in segue :%@",actualDataArray);
[new setMyrecipeTittleArray:actualDataArray];
}
}
关于ios - 在按钮操作之前执行prepareForSegue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36438956/