当前设计
我目前在我的 Storyboard上有一个 View Controller ,它有一个嵌入式选项卡 Controller 和一个嵌入式导航控件。此 View 控件还有一个容器 View ,该 View 显示一个 View ,该 View 取决于在位于导航栏中的分段控件中选择的值。
为容器 View 加载 View
容器 View 的 View 是 XIB 文件(不是基于 Storyboard的)并以编程方式加载:
- (void)viewDidLoad
{
[super viewDidLoad];
// First Controller
self.firstViewController = [[FirstViewController alloc] init];
// Second Controller
self.secondViewController = [[SecondViewController alloc] init];
// Add the controllers to an Array
self.controllers = @[self.firstViewController, self.secondViewController];
// Set the container to show the first view controller on load
[self displayContentController:[self.controllers firstObject]];
}
- (void)displayContentController:(UIViewController *)content
{
[self addChildViewController:content];
content.view.frame = [self frameForContentController];
[self.view addSubview:content.view];
[content didMoveToParentViewController:self];
// Set current controller
self.currentController = content;
}
我面临的问题
我遇到的问题是,容器 View 的其中一个 View 是包含照片的 Collection View ,当他们选择其中一个单元格时,我需要能够推送包含大尺寸图像的 View 。
如何在容器 View 内的 View 中将 View 推送到导航 Controller ?
最佳答案
您的 containerViewController 是否嵌入到 navigationController 中?
如果是这样,您可以简单地从 firstViewController
或 secondViewController
中调用 self.navigationController
它将向上遍历 parentViewController
链,直到到达 UINavigationController
(或子类) - 然后您将调用 -pushViewController:animated:
在那个导航 Controller 上。
事实上,对于您自己的 containerController,您还应该为 UIViewController
提供一个类别,它会为您的容器添加一个简单的 getter:
@interface UIViewController (YourContainer)
- (YourContainer *)yourContainer;
@end
@implementation UIViewController (YourContainer)
- (YourContainer *)yourContainer
{
if ([self isKindOfClass:[YourContainer class]]) {
return (YourContainer *)self;
}
UIViewController *parent = self.parentViewController;
while (! [parent isKindOfClass:[YourContainer class]] && parent != nil) {
parent = parent.parentViewController;
}
return (YourContainer *)parent;
}
@end
这将为您提供与 UINavigationController
和 UITabBarController
提供的相同的 childViewController 行为。事实上,如果您查看这些类的 header ,您会发现它们还通过 UIViewController
上的类别提供此功能——因此从现在开始,每个 childViewController 都可以调用 self.yourContainer
获取对您的自定义容器的引用,或者 nil,如果它不包含在一个容器中。
关于ios - 从容器 View 内的 View 将 View 推送到导航堆栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24952674/