【问题标题】:Bool value not being sent to destination ViewController布尔值未发送到目标 ViewController
【发布时间】:2014-10-17 05:52:44
【问题描述】:

我想从我的SearchViewController 发送一个布尔值didAddNewItemMatchCenterViewController,然后根据布尔值的状态运行一个函数。我尝试将YESdidAddNewItem 值发送到我的目的地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);
                                                                        }
                                                                    }];

                                    }
                                }];


     }


}

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

@property (assign) BOOL didAddNewItem;

【问题讨论】:

  • 什么时候推送 MatchCenterViewController?如果您在执行块之前推送此视图控制器,则不会为 didAddNewItem 分配值。
  • 这里的问题在于块,因为当执行 segue 时,无论调用什么块,都会调用 MatchCenterViewController 的 viewDidload 方法。所以你的 bool 值不会被设置,并且你的 viewDidlaod 方法被调用,它没有你的 bool 值。因此,请检查您的流程并进行更改。

标签: ios objective-c boolean


【解决方案1】:

在您的prepareForSegue 中,您正在异步调用callFunctionInBackground,这意味着很有可能segue 将完成并且新的视图控制器将在您在@ 的block 中设置didAddNewItem 之前呈现出来。 987654325@.

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

【讨论】:

  • 有道理。我在上面编辑了我的代码以显示我为响应您的回答所做的更改,但由于某种原因它仍然无法正常工作。它切换到 MatchCenterViewController,但不运行 if 语句中的代码,这意味着 _didAddNewItem 仍未设置为 YES。
  • 你必须给我们更多的东西才能继续下去。做一些额外的诊断来准确地找出它是否出了问题。我建议在每个完成块中设置一个断点,然后单步执行代码,看看哪里出错了。
猜你喜欢
  • 2011-03-28
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多