2014-10-17 50 views
0

我想从我的SearchViewController发送布尔值didAddNewItemMatchCenterViewController,然后根据布尔值的状态运行一个函数。我试图发送一个didAddNewItemYES到我的目的地,MatchCenterViewController,但它似乎没有正确发送,因为下面的函数从不运行。Bool值未被发送到目标ViewController

下面是我从SearchViewController发送它(编辑以反映罗布的回答):

- (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;

+0

你什么时候推MatchCenterViewController?如果您在exics块之前推送此视图控制器,则不会为didAddNewItem指定值。 – 2014-10-17 06:14:23

+0

这里的问题与当segue执行时的块一样,那么将调用你的MatchCenterViewController的viewDidload方法,而不管你被调用的块如何。所以你的布尔值不会被设置,并且你的viewDidlaod方法被调用,它不具有你的布尔值。因此请检查您的流量并在其中进行更改。 – nikhil84 2014-10-17 06:17:02

回答

2

在你prepareForSegue,您呼叫callFunctionInBackground异步的,这意味着它很可能是SEGUE将结束,新的视图控制器将被呈现在你的callFunctionInBackgroundblock设置didAddNewItem之前。

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

+0

有道理。我编辑了上面的代码,以显示我为回应您的答案所做的更改,但由于某些原因,它仍然无法正常工作。它切换到MatchCenterViewController,但不运行if语句中的代码,这意味着_didAddNewItem仍然没有被设置为YES。 – Ghobs 2014-10-18 21:25:56

+0

你必须给我们更多的去做。做一些额外的诊断,弄清楚它是否会出错。我建议在每个完成块内设置一个断点,然后单步执行代码并查看错误出现的位置。 – Rob 2014-10-18 21:31:42