简体   繁体   English

UIViewController类别不适用于viewDidLoad

[英]UIViewController category not working on viewDidLoad

I'm using a custom rootViewController, and am using the code below to access it from any UIViewController 我正在使用自定义的rootViewController,并且正在使用下面的代码从任何UIViewController访问它

@implementation UIViewController(CustomRootVC)

- (CustomRootVC*)customViewController
{
  UIViewController *parent = self;
  Class customClass = [CustomRootVC class];

  while ( nil != (parent = [parent parentViewController]) && ![parent customClass] )
  {
  }

  return (id)parent;
}

@end

If I call self.customViewController on viewDidLoad I get nil. 如果我在viewDidLoad上调用self.customViewController,我将得到零。 If I call it on willAppear I get the reference I expect. 如果我将其调用willAppear,则会得到我期望的参考。

I'm guessing this is something to do with the order I add the view controllers to my view controller container (ie viewDidLoad is called before the view controller has been added to customViewController and so it isn't a parent), but I can't spot anything obvious. 我猜想这与将视图控制器添加到视图控制器容器中的顺序有关(即,在将视图控制器添加到customViewController之前调用viewDidLoad,因此它不是父级),但是我可以没有发现任何明显的东西。 I add the view controllers as follows: 我将视图控制器添加如下:

- (void)addViewController:(UIViewController*)controller toWrapper:(PLSliderView*)wrapper{
  [self addChildViewController:controller];
  [self addView:controller.view ToWrapper:wrapper];
  [self.viewControllers addObject:controller];
  [controller didMoveToParentViewController:self];
}

In particular, the issue seems to be with adding a new view controller and view as follows: 特别地,问题似乎在于添加新的视图控制器和视图,如下所示:

- (void)replaceTopOfStackWithViewController:(UIViewController *)newController animated:  (BOOL)animated {
  UIViewController *oldController = self.currentController;
  [self addChildViewController:newController];
  [self transitionFromViewController:oldController
                toViewController:newController
                        duration:1.0
                         options:UIViewAnimationOptionTransitionCrossDissolve
                      animations:nil
                      completion:^(BOOL finished) {                                    
                        [self.rightViewController didMoveToParentViewController:self];
                        [self removeViewController:oldController];
                        [self queryDimensions:@"REPLACE"];
                        [self.view setUserInteractionEnabled:YES];
                        self.currentController = newController
                      }];

} }

this piece of code (parent = [parent parentViewController]) sets parent equal to self.parentViewController . 这段代码(parent = [parent parentViewController])parent设置为self.parentViewController If self does not have a parent view controller at the time this is called, it would return nil as expected. 如果self在调用时没有父视图控制器,则它将按预期返回nil

more broadly, I'm not sure what you're trying to accomplish with this code, or why you need to use a category. 更广泛地说,我不确定您要使用此代码完成什么工作,或者不确定为什么需要使用类别。 it seems like that sort of behavior would make more sense in a UIViewController subclass. UIViewController子类中,这种行为似乎更有意义。

在调用viewDidLoad时,尚未将视图控制器添加到视图控制器层次结构中,因此它没有父视图控制器,因此您的函数按预期返回nil。

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

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