简体   繁体   English

UINavigationController不会在推送的UIViewController上调用viewDidLoad

[英]UINavigationController does not call viewDidLoad on pushed UIViewController

I'm facing an odd problem concerning pushing a UIViewController into a UINavigationController. 我面临有关将UIViewController推入UINavigationController的奇怪问题。 CustomTableViewController is a subclass of UITableViewController CustomTableViewControllerUITableViewController的子类

CustomTableViewController* vc = [[CustomTableViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:vc];
[self.view addSubview:navVC.view];

For some reason, the Navigation Controller is loaded and visible on screen but viewDidLoad is never called on CustomTableViewController . 出于某种原因,导航控制器已加载并在屏幕上可见,但从未CustomTableViewController调用viewDidLoad

Manually pushing the VC afterwards also does not work (still not loading the vc 's view) 之后手动推送VC也无效(仍然无法加载vc的视图)

  • [navVC pushViewController:vc animated:YES];

However, the following methods DO call viewDidLoad on vc : 但是,下面的方法viewDidLoadvc

  1. [self.view addSubview:vc.view];
  2. [self presentViewController:navVC animated:YES completion:nil];
  3. [self presentViewController:vc animated:YES completion:nil];

So I tend to believe that there is nothing wrong with the View Controller nor the Navigation Controller. 因此,我倾向于认为View Controller或Navigation Controller没有任何问题。 However, I have no clue why the view is not being loaded. 但是,我不知道为什么未加载视图。 Manually forcing [vc view] will load the view but then the other view lifecycle methods are not called. 手动强制[vc view]将加载该视图,但是不会调用其他视图生命周期方法。

There is also nothing wrong with CustomTableViewController , as using UITableViewController itself also doesn't work. CustomTableViewController也没有任何问题,因为使用UITableViewController本身也不起作用。

Any ideas? 有任何想法吗?

You need to add it as child view controller 您需要将其添加为子视图控制器

    [childController willMoveToParentViewController:rootViewController];
    [rootViewController addChildViewController:childController];
    [rootViewController.view addSubview:childController.view];
    [childController didMoveToParentViewController:rootViewController];

Hope this works 希望这行得通

I suspect you're getting issues because it seems you are trying to make a custom container view controller. 我怀疑您遇到问题,因为似乎您正在尝试制作自定义容器视图控制器。

Read up on Apple's guide on custom container view controllers. 阅读有关自定义容器视图控制器的Apple指南。

From the guide, here are some example methods to add and remove child view controllers: 在指南中,以下是添加和删除子视图控制器的一些示例方法:

Swift 3.0: Swift 3.0:

func display(contentController content: UIViewController) {
    addChildViewController(content)

    // Here, frameForContentController is just some function you define
    // that calculates and returns the frame of the content controller's view.
    content.view.frame = frameForContentController()

    view.addSubview(content.view)
    content.didMove(toParentViewController: self)
}

func hide(contentController content: UIViewController) {
    content.willMove(toParentViewController: nil)
    content.view.removeFromSuperview()
    content.removeFromParentViewController()
}

Objective-C: 目标C:

- (void)displayContentController:(UIViewController *)content
{
   [self addChildViewController:content];

   // Here, [self frameForContentController] is just some method you define
   // that calculates and returns the frame of the content controller's view.
   content.view.frame = [self frameForContentController];

   [self.view addSubview:content.view];
   [content didMoveToParentViewController:self];
}

- (void)hideContentController:(UIViewController *)content
{
   [content willMoveToParentViewController:nil];
   [content.view removeFromSuperview];
   [content removeFromParentViewController];
}

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

相关问题 如何不在 uinavigationcontroller 中调用 viewdidload? - how to not call viewdidload in uinavigationcontroller? 在动画完成之前,翻转过渡UINavigationController推送不会调用viewDidLoad - Flip transition UINavigationController push does not call viewDidLoad until animation completed UITabBarController的TabBar阻止由其托管的UINavigationController推送的UIViewController - UITabBarController's TabBar blocking UIViewController that is pushed by UINavigationController it hosts UIViewController中的ViewDidLoad方法 - 什么时候被调用? - ViewDidLoad method in UIViewController - when does it get called? UIViewController viewDidLoad - UIViewController viewDidLoad UIViewController或UINavigationController是否设置了它的视图大小? - Does UIViewController or UINavigationController set the size of it's view? 如何在iOS上的静态库中调用UIViewController :: viewDidLoad? - How to call UIViewController::viewDidLoad from inside a static library on iOS? iOS中的ViewDidLoad和UINavigationController? - ViewDidLoad and UINavigationController in iOS? 如何从已经在UINavigationController堆栈上的UIViewController调用方法 - How to call a method from a UIViewController thats already on the UINavigationController stack UIViewController中的插座在viewdidload中为nil - outlets in UIViewController nil in viewdidload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM