kernelpanic kernelpanic - 2 years ago 135
iOS Question

UITableView segue within the same ViewController

I have a UIViewController that contains both, a map and a tableView which are connected together and it looks something like this:

enter image description here

And I'd like to be able to push the tableView to another tableView within the same view controller so that the map stays where is it. Of course I'd like to be able to unwind the segue backwards programmatically with a button that'll appear in the navbaritem in the top left.

Similar behaviour can be observed in the iPad tableView controllers placed in a popover. How would I go about doing this in Swift (iOS 8) ?

Any code snippets or pointers to existing code examples are greatly appreciated.

Answer Source

You can achieve view controller containment like so:

- (void)viewDidLoad {
        [super viewDidLoad];

        // Setup the container view
        _containerView = [[UIView alloc] initWithFrame: // DESIRED FRAME];
        _containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:_containerView];

        // Setup the main view controller
        [self addChildViewController:_mainViewController];
        _mainViewController.view.frame = _containerView.bounds;
        [_containerView addSubview:_mainViewController.view];
        [_mainViewController didMoveToParentViewController:self];

Now you can create both table views in separate view controllers. It sounds like you should add a navigation controller ("mainViewController" in code snippet) to your main view controller so you can "push" a new table onto the stack.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download