Unbalanced calls to begin/end appearance transitions for

后端 未结 22 1952
长情又很酷
长情又很酷 2020-11-28 19:23

I read SO about another user encountering similar error, but this error is in different case.

I received this message when I added a View Controller initially:

22条回答
  •  一整个雨季
    2020-11-28 19:58

    As @danh suggested, my issue was that I was presenting the modal vc before the UITabBarController was ready. However, I felt uncomfortable relying on a fixed delay before presenting the view controller (from my testing, I needed to use a 0.05-0.1s delay in performSelector:withDelay:). My solution is to add a block that gets called on UITabBarController's viewDidAppear: method:

    PRTabBarController.h:

    @interface PRTabBarController : UITabBarController
    
    @property (nonatomic, copy) void (^viewDidAppearBlock)(BOOL animated);
    
    @end
    

    PRTabBarController.m:

    #import "PRTabBarController.h"
    
    @implementation PRTabBarController
    
    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        if (self.viewDidAppearBlock) {
            self.viewDidAppearBlock(animated);
        }
    }
    
    @end
    

    Now in application:didFinishLaunchingWithOptions:

    PRTabBarController *tabBarController = [[PRTabBarController alloc] init];
    
    // UIWindow initialization, etc.
    
    __weak typeof(tabBarController) weakTabBarController = tabBarController;
    tabBarController.viewDidAppearBlock = ^(BOOL animated) {
        MyViewController *viewController = [MyViewController new];
        viewController.modalPresentationStyle = UIModalPresentationOverFullScreen;
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
        [weakTabBarController.tabBarController presentViewController:navigationController animated:NO completion:nil];
        weakTabBarController.viewDidAppearBlock = nil;
    };
    

提交回复
热议问题