简体   繁体   English

在viewDidAppear之后没有调用didShowViewController

[英]didShowViewController not being called after viewDidAppear

I'm using the UINavigationController delegate methods on a custom nav controller subclass to provide to special UI behaviour. 我在自定义导航控制器子类上使用UINavigationController委托方法来提供特殊的UI行为。 Unfortunately in some situations I am not receiving the didShowViewController callback after pushing a view, even though that view becomes visible. 不幸的是,在某些情况下,即使该视图变为可见,在推送视图后我也没有收到didShowViewController回调。

I am receiving some of the delegate calls and most of the time things work fine - only with specific View controllers in my app do I see this behaviour. 我收到了一些委托调用,并且在大多数情况下都可以正常工作-仅在应用程序中使用特定的View控制器时,我才能看到此行为。

So, after creating a few tests, I found that the problem can be caused by presenting another view controller from the didAppear or willAppear methods. 因此,在创建了一些测试之后,我发现问题可能是由didAppear或willAppear方法中的另一个视图控制器引起的。 The latter seems fine but the missing call to didShowViewController after viewDidAppear seems like a bug to me. 后者似乎很好,但是在viewDidAppear之后缺少对didShowViewController的调用对我来说似乎是个错误。

to avoid this issue you could create a completion block in a subclass of UINavigationController for pushing view controllers and only start your pushed view controller off once the push is complete. 为避免此问题,您可以在UINavigationController的子类中创建一个完成块来推送视图控制器,并且仅在推送完成后才启动被推送的视图控制器。 Something similar to this perhaps: Completion handler for UINavigationController "pushViewController:animated"? 可能与此类似: UINavigationController的完成处理程序“ pushViewController:animated”?

For example (warning! untested): 例如(警告!未经测试):

@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

You could probably do a bunch more to synchronise the completion block and the pushedVC.... 您可能还可以做更多的工作来同步完成块和pushVC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM