简体   繁体   English

viewDidLoad和ViewDidAppear没有被调用

[英]viewDidLoad and ViewDidAppear not being called

I have a UIViewController that is being created from a xib file. 我有一个从xib文件创建的UIViewController

I initialise it like this: 我这样初始化:

 _sharedSingleton = [[SlipChangesViewController alloc] init];
            [[NSBundle mainBundle] loadNibNamed:@"SlipChangesViewController" owner:_sharedSingleton options:nil];

I add the view of that UIViewController to the subview in another UIViewController: 我将该UIViewController的视图添加到另一个UIViewController的子视图中:

[self.view addSubview:slipChangesVC.view];

Now the weird thing is, the view is successfully added to the subview, but for some reason both viewDidLoad and vieWillAppear are not called. 现在奇怪的是,该视图已成功添加到子视图中,但是由于某种原因,未同时调用viewDidLoadvieWillAppear It is however successfully calling the method to create the shared singleton and the init method. 但是,它已成功调用该方法来创建共享单例和init方法。

I have double checked to make sure: 我已仔细检查以确保:

  • None of the UIViews or UIViewControllers are nil 没有UIViewsUIViewControllers为零
  • The outlets from the xib are correctly linked Xib的插座正确连接
  • I am calling [super viewDidLoad] and [super viewWillAppear] 我正在呼叫[super viewDidLoad][super viewWillAppear]

I have check various threads on stackOverflow and tried there suggestions with no success. 我检查了stackOverflow上的各种线程,并尝试了没有成功的建议。 eg. 例如。 Calling [slipChangesVC view] 调用[slipChangesVC view]

Does anyone know why these 2 methods would not be called? 有谁知道为什么不会调用这两种方法?

This is not the right way of adding child view controller. 这不是添加子视图控制器的正确方法。 In this way, view owner gets changes. 这样,视图所有者即可获得更改。 View detached from its view controller and view controller stops receiving viewDidLoad and other view methods. 从其视图控制器分离的视图,并且视图控制器停止接收viewDidLoad和其他视图方法。

If you want to make it work, please look into already answered question: add UIViewController in subview 如果要使其工作,请查看已经回答的问题: 在子视图中添加UIViewController

You are adding a SlipChangesViewController view over existing view. 您正在现有视图之上添加一个SlipChangesViewController视图。 In that case viewDidLoad and viewWillAppear never called. 在那种情况下,从未调用过viewDidLoad和viewWillAppear。 You need to add a ViewController for this 您需要为此添加一个ViewController

try this one 试试这个

- (void)addChildViewC:(UIViewController *)childViewC
{
    [self addChildViewController:childViewC];
    childViewC.view.frame = self.view.bounds;
    [self.view addSubview:childViewC.view];
    [childViewC didMoveToParentViewController:self];
}

I would never make a view controller a singleton. 我永远不会使视图控制器成为一个单例。 It's not a singleton. 这不是一个单身人士。 It gets created, destroyed, created again, destroyed again. 它被创建,破坏,再次创建,再次破坏。

I think viewDidLoad is only called once during the lifetime of a view controller. 我认为viewDidLoad在视图控制器的生命周期内仅被调用一次 So making yours a singleton could cause all kinds of problems. 因此,使您的单身人士可能会导致各种问题。

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

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