Bool值未发送到目标ViewController

时间:2014-10-17 05:52:44

标签: ios objective-c boolean

我想从我的didAddNewItemSearchViewController发送一个bool值MatchCenterViewController,然后根据bool值的状态运行一个函数。我尝试将didAddNewItemYES发送到我的目的地MatchCenterViewController,但它似乎无法正确发送,因为下面的函数永远不会运行。

以下是我如何从SearchViewController发送它(编辑以反映Rob的答案):

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ShowMatchCenterSegue"]) {

        _didAddNewItem = YES;

        MatchCenterViewController *controller = (MatchCenterViewController *) segue.destinationViewController;

        NSLog(@"we're about to set controller values before segueing to MC");
        // Send over the matching item criteria
        controller.itemSearch = self.itemSearch.text;
        controller.matchingCategoryId = self.matchingCategoryId1;
        controller.matchingCategoryMinPrice = self.matchingCategoryMinPrice1;
        controller.matchingCategoryMaxPrice = self.matchingCategoryMaxPrice1;
        controller.matchingCategoryCondition = self.matchingCategoryCondition1;
        controller.matchingCategoryLocation = self.matchingCategoryLocation1;
        controller.itemPriority = self.itemPriority;

        [self.tabBarController setSelectedIndex:1];
    }
}

这是我尝试在目的地中使用它的地方,MatchViewController:

- (void)viewDidAppear:(BOOL)animated
{

    if (_didAddNewItem == YES) {
    NSLog(@"well then lets refresh the MC");

    // Start loading indicator
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityIndicator.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);
    [self.view addSubview: activityIndicator];
    [activityIndicator startAnimating];

    // Disable ability to scroll until table is MatchCenter table is done loading
    self.matchCenter.scrollEnabled = NO;
    _matchCenterDone = NO;

    // 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,
                                        @"categoryId": self.matchingCategoryId,
                                        @"minPrice": self.matchingCategoryMinPrice,
                                        @"maxPrice": self.matchingCategoryMaxPrice,
                                        @"itemCondition": self.matchingCategoryCondition,
                                        @"itemLocation": self.matchingCategoryLocation,
                                        @"itemPriority": self.itemPriority,
                                        }
                                block:^(NSString *result, NSError *error) {

                                    if (!error) {
                                        NSLog(@"'%@'", result);
                                        self.matchCenterArray = [[NSArray alloc] init];

                                        [PFCloud callFunctionInBackground:@"MatchCenter3"
                                                           withParameters:@{}
                                                                    block:^(NSArray *result, NSError *error) {

                                                                        if (!error) {
                                                                            _matchCenterArray = result;
                                                                            [_matchCenter reloadData];
                                                                            [activityIndicator stopAnimating];

                                                                            // Reenable scrolling/reset didAddNewItem bool
                                                                            _matchCenterDone = YES;
                                                                            self.matchCenter.scrollEnabled = YES;
                                                                            //_didAddNewItem = NO;
                                                                            NSLog(@"Result: '%@'", result);
                                                                        }
                                                                    }];

                                    }
                                }];


     }


}

我确保它已正确设置为两个ViewControllers标头中的属性,因此我不确定为什么它没有正确设置目标VC中的值。我知道addToMatchCenter函数正确运行而没有错误,因此它应该正常工作。

@property (assign) BOOL didAddNewItem;

1 个答案:

答案 0 :(得分:2)

prepareForSegue中,您异步调用callFunctionInBackground,这意味着在您设置didAddNewItem之前,segue很可能会完成并且新的视图控制器将很好地显示block的{​​{1}}。

我倾向于更改目标控制器以启动此异步请求本身,但让它显示callFunctionInBackground(或其他内容)以表明依赖请求尚未完成,然后在UIActivityIndicatorView中,您可以删除活动指示器视图并相应地更新UI。