简体   繁体   中英

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

故事板

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!

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

示例故事板


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:

模拟器演示

I was needing exactly what @vacawama proposed here, though I couldn't reproduce that, I tried exactly your steps but self.delegate?.segueToNext("GoToGreen") got called but neither the protocol itself nor the container view controller. After an entire day searching about this approach I realized the problem was with the swift version. Just replace this:

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

for this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "EmbedH" {
        let dvc = segue.destination as! HViewController
        dvc.delegate = self
    }
}

Other detail I was missing was about the embedded segue. Be sure to connect the container View to the HViewController, not the View Controller itself, otherwise the Embed option for segue won't appear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM