在viewDidAppear之后没有调用didShowViewController

时间:2014-02-26 14:57:08

标签: ios objective-c

我在自定义导航控制器子类上使用UINavigationController委托方法来提供特殊的UI行为。不幸的是,在某些情况下,我在推送视图后没有收到didShowViewController回调,即使该视图变得可见。

我正在接收一些委托调用,并且大多数情况下工作正常 - 只有我的应用程序中的特定View控制器才能看到此行为。

1 个答案:

答案 0 :(得分:3)

因此,在创建了一些测试后,我发现问题可能是由didAppear或willAppear方法提供另一个视图控制器引起的。后者看起来很好但是在viewDidAppear之后缺少对didShowViewController的调用对我来说似乎是个错误。

要避免此问题,您可以在UINavigationController的子类中创建一个完成块,用于推送视图控制器,并且只有在推送完成后才启动推送的视图控制器。也许类似的东西: Completion handler for UINavigationController "pushViewController:animated"?

例如(警告!未经测试):

@interface PbNavigationController : UINavigationController <UINavigationControllerDelegate>

@property (nonatomic,copy) dispatch_block_t completionBlock;
@property (nonatomic,strong) UIViewController * pushedVC;

@end


@implementation PbNavigationController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.delegate = self;
    }
    return self;
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    NSLog(@"didShowViewController:%@", viewController);

    if (self.completionBlock && self.pushedVC == viewController) {
        self.completionBlock();
    }
    self.completionBlock = nil;
    self.pushedVC = nil;
}

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.pushedVC != viewController) {
        self.pushedVC = nil;
        self.completionBlock = nil;
    }
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(dispatch_block_t)completion {
    self.pushedVC = viewController;
    self.completionBlock = completion;
    [self pushViewController:viewController animated:animated];
}

@end

你可以做更多的事情来同步完成块和pushVC ....