[英]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
CustomTableViewController
是UITableViewController
的子类
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
: 但是,下面的方法做电
viewDidLoad
的vc
:
[self.view addSubview:vc.view];
[self presentViewController:navVC animated:YES completion:nil];
[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.