Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform segue from container view within a view displayed by navigation controller?

Tags:

xcode

ios

swift

Storyboard

I want to segue from a view container within "H" that is presented using the navigation controller connected to the Split View Controller. How can I accomplish this? I have tried regular performSegueWithIdentifier using locally linked storyboard ID's but that removes the top navigation bar. I want to retain the top navigation bar and execute the segue as if it was done using the master navigation controller (rows that select which view controller is being presented in the detail view).

Any help is greatly appreciated!

like image 410
Plastus Avatar asked Jun 01 '16 14:06

Plastus


1 Answers

Here is an example of how to perform a segue from an embedded ViewController.

Sample Storyboard


ViewController.swift

import UIKit

protocol SegueHandler: class {
    func segueToNext(identifier: String)
}

class ViewController: UIViewController, SegueHandler {

    func segueToNext(identifier: String) {
        self.performSegueWithIdentifier(identifier, sender: self)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "EmbedH" {
            let dvc = segue.destinationViewController as! HViewController
            dvc.delegate = self
        }
    }
}

HViewController.swift

import UIKit

class HViewController: UIViewController {

    weak var delegate: SegueHandler?

    @IBAction func pressH(sender: UIButton) {
        delegate?.segueToNext("GoToGreen")
    }

}

Setup:

  1. Use delegation to have the HViewController tell its embedding viewController to perform the segue.
  2. Create a protocol called SegueHandler which just describes a class that implements the method segueToNext(identifier: String).

    protocol SegueHandler: class {
        func segueToNext(identifier: String)
    }
    
  3. Make your viewController implement this protocol by adding it to the class declaration line:

    class ViewController: UIViewController, SegueHandler {
    

    and by implementing the required function.

  4. Add a delegate property to HViewController:

    weak var delegate: SegueHandler?
    
  5. Click on the embed segue arrow between ViewController and HViewController. Give it the identifier "EmbedH" in the Attributes Inspector.

  6. Create a show segue between ViewController and the GreenViewController by Control dragging from the viewController icon at the top of ViewController to the GreenViewController. Name this segue "GoToGreen" in the Attributes Inspector.

  7. In prepareForSegue for ViewController, when the "EmbedH" segue happens, set the delegate property of HViewController to self (ViewController).

  8. When the user clicks the H button in the HViewController, call delegate?.segueToNext("GoToGreen") to trigger the segue in ViewController.


Here it is running in the simulator:

Simulator Demonstration

like image 138
vacawama Avatar answered Sep 30 '22 07:09

vacawama