所以我有一个表视图,在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。
答案 0 :(得分: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
之上安装了另一个视图控制器,它就不会推送。并且还要确保你在主线程上做了所有这些。
答案 1 :(得分:0)
有很多方法可以做到 其中一个是
取一个实例BOOL isRowSelectedToViewPush
; // set 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];
}];
}
}