ios - 从容器 View 内的 View 将 View 推送到导航堆栈?

标签 ios objective-c uinavigationcontroller uicontainerview

当前设计

我目前在我的 Storyboard上有一个 View Controller ,它有一个嵌入式选项卡 Controller 和一个嵌入式导航控件。此 View 控件还有一个容器 View ,该 View 显示一个 View ,该 View 取决于在位于导航栏中的分段控件中选择的值。

Storyboard 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 中?

如果是这样,您可以简单地从 firstViewControllersecondViewController 中调用 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

这将为您提供与 UINavigationControllerUITabBarController 提供的相同的 childViewController 行为。事实上,如果您查看这些类的 header ,您会发现它们还通过 UIViewController 上的类别提供此功能——因此从现在开始,每个 childViewController 都可以调用 self.yourContainer 获取对您的自定义容器的引用,或者 nil,如果它不包含在一个容器中。

关于ios - 从容器 View 内的 View 将 View 推送到导航堆栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24952674/

相关文章:

ios - 通过越狱加载的应用程序的文档文件夹的位置

ios - Iphone 在 prefix.pch 中写入代码

objective-c - 什么是 CG 光栅数据?

objective-c - WebView 中的后退/前进滑动手势

ios - 在同时具有标签栏 Controller 和导航 Controller 的屏幕中放置广告的位置

ios - 当它们是两个 View 之间的导航 Controller 时,如何将 managedObjectContext 从 appDelegate 传递到第一个 ViewController

ios - 如何为 UITextField 分配默认值?

php - 某些设备未收到 Apple 推送通知

ios - RSA 公钥生成——Swift

swift - 使用 Swift 以编程方式设置 UINavigationController 的 UINavigationBar