【问题标题】:How to push a view controller safely from a completion block?如何从完成块安全地推送视图控制器?
【发布时间】:2014-08-12 09:09:09
【问题描述】:

所以我有一个表视图,并且在 didSelectRowAtIndexPath 方法中,我想根据选择的行推送一个视图控制器。但是,要确定要推送哪个视图,我需要执行 Parse(我正在为我的应用程序使用 parse.com 后端)后台获取,并且我想在此查询的完成处理程序中执行推送。但是,问题在于,如果我多次点击该按钮,则在完成后,多个视图会被推送到我的导航堆栈上,代码如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// call super because we're a custom subclass.
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
UserDetailsViewController *controller = [[DetailsViewController alloc] initWithNibName:nil
                                                                                    bundle:nil];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query includeKey:@"key1"];
[query whereKey:@"key2" containsString:((UILabel *)(cell.contentView.subviews[1])).text];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
    Model *model = [[Model alloc] initWithPFObject:[objects objectAtIndex:0]];
    controller.annotation = model;
    [self.navigationController pushViewController:controller animated:YES];
}];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

我如何锁定完成块以仅执行一次推送,例如第一个点击的 indexPath,即使用户在推送发生之前一个接一个地点击许多行。

【问题讨论】:

    标签: ios objective-c uitableview uinavigationcontroller parse-platform


    【解决方案1】:

    尝试类似:

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
        if (self.navigationController.topViewController == self) {
            Model *model = [[Model alloc] initWithPFObject:[objects objectAtIndex:0]];
            controller.annotation = model;
            [self.navigationController pushViewController:controller animated:YES];
        }
    }];
    

    这样,如果您在 self 之上已经有另一个视图控制器,它就不会推送。并确保您在主线程上执行所有这些操作。

    【讨论】:

      【解决方案2】:

      有很多方法可以做到这一点 其中之一是

      举个例子BOOL isRowSelectedToViewPush; // 设置 isRowSelectedToViewPush = NO;在 viewWillAppear 方法中

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
          // call super because we're a custom subclass.
          if(!isRowSelectedToViewPush ) {
          isRowSelectedToViewPush = YES;
          [super tableView:tableView didSelectRowAtIndexPath:indexPath];
          UserDetailsViewController *controller = [[DetailsViewController alloc] initWithNibName:nil
                                                                                              bundle:nil];
          UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
      
          PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
          [query includeKey:@"key1"];
          [query whereKey:@"key2" containsString:((UILabel *)(cell.contentView.subviews[1])).text];
          [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
              Model *model = [[Model alloc] initWithPFObject:[objects objectAtIndex:0]];
              controller.annotation = model;
              [self.navigationController pushViewController:controller animated:YES];
          [tableView deselectRowAtIndexPath:indexPath animated:YES];
      
          }];
      
          }
       }
      

      【讨论】:

        猜你喜欢
        • 2014-03-11
        • 2012-12-22
        • 2010-12-11
        • 2016-10-22
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 2015-05-23
        相关资源
        最近更新 更多