ios - 在按钮操作之前执行prepareForSegue

标签 ios json uitableview segue viewcontroller

我是 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/

相关文章:

ios - 在 iDevice 上呈现数学公式

ios - 出现此错误时如何退出facebook sdk登录?

javascript - 解析来自 API 的响应

JavaScript 缺少参数

ios - CoreGraphics (drawRect) 用于在 UITableViewCell 中绘制标签和 UIImageView

iphone - 来自 NSFetchedResultsController 的索引结果

iOS - Swift 中 UITableViewCells 的左右边距

iphone - 我可以在系统 (""内使用 NSString 吗?

ios - Cordova(Phonegap)ajax调用卡在iphone设备上

ruby-on-rails - Rails 编辑序列化的 JSON 数据