0

Current Design

I currently have a view controller on my storyboard that has an embedded tab controller and an embedded navigation control. This view control also has a container view that shows a view dependent upon the value selected in the segmented control located in the navigation bar.

Storyboard View

Loading Views for the Container View

The views for the container view are XIB files (not storyboard based) and are loaded programmatically:

- (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;
}

The Problem I Face

The problem I have is that one of the views for the container view is a collection view that contains photos and I need to be able to push a view containing an image in large size when they select one of the cells.

How do I go about pushing a view onto the navigation controller in a view that is inside the container view?

4

2 回答 2

2

Is your containerViewController embedded inside a navigationController ?

If so, you can simply call self.navigationController from within firstViewController or secondViewController

It will traverse the parentViewController chain upwards until it reaches a UINavigationController (or subclass) - then you would call -pushViewController:animated: on that navigationController.

In fact for your own containerController, you should also provide a category for UIViewController which adds a simple getter for your container:

@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

This will give you the same behavior for childViewControllers as UINavigationController and UITabBarController provide. In fact, if you have a look at the headers for these classes, you'll see that they also provide this functionality through a category on UIViewController -- so from now on, every childViewController can call self.yourContainer to get a reference to your custom container, or nil, if it is not contained in one.

于 2014-07-25T10:03:25.710 回答
0

Add a navigation controller before your history feedback item,

To add Navigation controller,

1.Select your history feedback item controller,

2.From `Editor > Embed In > NavigationController like below Image:

enter image description here

Now in Your cell/photo selection method use

[self.navigationController pushViewController:YourUIViewController  animated:YES];
于 2014-07-25T10:00:02.860 回答