完成块方法后执行方法

时间:2017-12-07 12:20:22

标签: ios objective-c callback grand-central-dispatch objective-c-blocks

我正在使用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解决方案,比如在队列中添加方法并执行但是我得到了异常

  

“主线程检查器:在后台线程上调用UI API: - [UIView bounds]”

然后我尝试this too得到了与上面相同的错误。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:-1)

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

看起来有点像这样。

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