在一个块方法完成后执行一个方法

Execute a method after the completion of a block method

我正在为左菜单使用 RNFrostedSidebar。关闭侧边栏时,下面的代码块正在执行

- (void)handleTap:(UIPanGestureRecognizer *)recognizer {
    [self dismissAnimated:YES completion:nil];
}

- (void)dismissAnimated:(BOOL)animated completion:(void (^)(BOOL finished))completion {

    void (^completionBlock)(BOOL) = ^(BOOL finished){
        [self rn_removeFromParentViewControllerCallingAppearanceMethods:YES];

        rn_frostedMenu = nil;

        if (completion) {
            completion(finished);
        }
    };

    if (animated) {
        CGFloat parentWidth = self.view.bounds.size.width;
        CGRect contentFrame = self.contentView.frame;
        contentFrame.origin.x = self.showFromRight ? parentWidth : -_width;

        CGRect blurFrame = self.blurView.frame;
        blurFrame.origin.x = self.showFromRight ? parentWidth : 0;
        blurFrame.size.width = 0;
        self.blurView.frame = blurFrame;
        self.blurView.alpha=0.1;

        [UIView animateWithDuration:self.animationDuration delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
                             self.viewForTable.frame = contentFrame;
                     }
                     completion:completionBlock];
    } else {
        completionBlock(YES);
    }
  }

执行 dismissAnimated 方法后,我想 运行 更新 HomeViewController 的代码,该代码是

[self callNavBar];

-(void) callNavBar {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"adjustNavBar"
                                                    object:self];
}

我尝试了 this 解决方案,比如在队列中添加方法并执行但我得到异常

"Main Thread Checker: UI API called on a background thread: -[UIView bounds]"

然后我试了 this too 得到了和上面一样的错误。

您可以将函数作为完成处理程序放在此处,而不是 nil。 这样它就会 运行 正好在您需要的时候。

看起来有点像这样。

- (void)handleTap:(UIPanGestureRecognizer *)recognizer {
      [self dismissAnimated:YES completion:[self callNavBar]];
}

您的问题是因为您没有在 UI 线程内更新 ui。要解决它,请在主队列中调用 dismissAnimated:completion: 方法。

- (void)handleTap:(UIPanGestureRecognizer *)recognizer {
  dispatch_async(dispatch_get_main_queue(), ^{
    [self dismissAnimated:YES completion:^(BOOL finished) {
      [self callNavBar];
    }];
  });
}