如何在另一个方法中访问由解析云代码返回的变量

时间:2014-05-31 22:24:22

标签: ios objective-c

我有一种方法:

- (IBAction)nextButton:(id)sender
{
    if (self.itemSearch.text.length > 0) {
        [PFCloud callFunctionInBackground:@"eBayCategorySearch"
                           withParameters:@{@"item": self.itemSearch.text}
                                    block:^(NSDictionary *result, NSError *error) {
                                        NSLog(@"'%@'", result);

                                       // Parses results

                                            NSArray *resultArray = [result objectForKey:@"results"];

                                                // Number of Top Categories
                                                NSDictionary *dictionary0 = [resultArray objectAtIndex:0];
                                                NSNumber *numberOfTopCategories = [dictionary0 objectForKey:@"Number of top categories"];

                                                // Ids of the Top Categories
                                                NSDictionary *dictionary1 = [resultArray objectAtIndex:1];
                                                NSArray *topCategoryIdsArray = [dictionary1 objectForKey:@"Top category Ids"];

                                                // Names of the Top Categories
                                                NSDictionary *dictionary2 = [resultArray objectAtIndex:2];
                                                NSArray *topCategoryNamesArray = [dictionary2 objectForKey:@"Top category names"];

                                                // Number of Top Categories matching User Categories
                                                NSDictionary *dictionary3 = [resultArray objectAtIndex:3];
                                                NSNumber *numberOfMatches = [dictionary3 objectForKey:@"Number of matches"];

                                                // Names of Top Categories matching User Categories
                                                NSDictionary *dictionary4 = [resultArray objectAtIndex:5];
                                                NSString *matchingCategoryCondition = [dictionary4 objectForKey:@"Matching Category Condition"];




                                                // Defines where each topCategory name will come from
                                                self.topCategory1 = [topCategoryNamesArray objectAtIndex:0];
                                            if ([numberOfTopCategories intValue] == 2) {
                                                self.topCategory2 = [topCategoryNamesArray objectAtIndex:1];
                                            }

                                                // Defines where each topCategory ID will come from
                                                self.topCategoryId1 = [topCategoryIdsArray objectAtIndex:0];
                                            if ([numberOfTopCategories intValue] == 2) {
                                                self.topCategoryId2 = [topCategoryIdsArray objectAtIndex:1];
                                            }


                                        if (!error) {

                                        // Decides which segue is taken based on results

                                            // if 1 match found clear categoryResults and top2 array
                                            if ([numberOfMatches intValue] == 1 ){
                                                [self performSegueWithIdentifier:@"ShowMatchCenterSegue" sender:self];
                                            }

                                            // if 2 matches found
                                            else if ([numberOfMatches intValue] == 2){
                                                [self performSegueWithIdentifier:@"ShowUserCategoryChooserSegue" sender:self];
                                                //default to selected categories criteria  -> send to matchcenter -> clear categoryResults and top2 array
                                            }

                                            // if no matches found, and 1 top category is returned
                                            else if ([numberOfMatches intValue] == 0 && [numberOfTopCategories intValue] == 1) {
                                                [self performSegueWithIdentifier:@"ShowCriteriaSegue" sender:self];
                                            }

                                            // if no matches are found, and 2 top categories are returned
                                            else if ([numberOfMatches intValue] == 0 && [numberOfTopCategories intValue] == 2) {
                                                [self performSegueWithIdentifier:@"ShowSearchCategoryChooserSegue" sender:self];
                                            }

                                        }
                                    }];
    }
}

调用Parse云代码函数,并返回各种值。特别是matchingCategoryCondition的一个值,我想在prepareForSegue方法中使用,如下所示:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([segue.identifier isEqualToString:@"ShowMatchCenterSegue"]) {
        MatchCenterViewController *controller = (MatchCenterViewController *) segue.destinationViewController;


        // Add new item to MatchCenter Array with the criteria from the matching userCategory instance, plus the search term
        [PFCloud callFunctionInBackground:@"addToMatchCenter"
                           withParameters:@{@"searchTerm": self.itemSearch.text,
//                                            @"categoryId": self.matchingCategoryCondition,
//                                            @"minPrice": self.minPrice,
//                                            @"maxPrice": self.maxPrice,
                                         @"itemCondition": matchingCategoryCondition,
//                                        @"itemLocation": self.itemLocation
                                            }
                                    block:^(NSString *result, NSError *error) {

                                        if (!error) {
                                            NSLog(@"'%@'", result);
                                        }
                                    }];



        // Send over the search query
        controller.itemSearch = self.itemSearch.text;
    }    
}

但是,我收到一条错误消息,指出matchingCategoryCondition是未声明的标识符。如何在这样的方法中使用它?

1 个答案:

答案 0 :(得分:2)

NobodyNada建议的一种方法是使用财产。一种稍微简单的方法是利用添加发送者参数的performSegue变种:

[self performSegueWithIdentifier:@"whatever" sender:matchingCategoryCondition];

这将在您准备segue方法的发件人参数中提供:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    NSString *matchingCategoryCondition = (NSString *)sender;
    // ...

但代码可能有另一个问题,即在segue期间异步解析调用的性能。你什么时候打算完成这个电话?目标视图控制器是否依赖于结果?如果是这样,你需要在performSegue之前启动该请求:然后从它的完成块中调用performSegue。