xcode 如何在导航控制器显示的视图中从容器视图执行转场?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37571139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:51:23  来源:igfitidea点击:

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

iosxcodeswift

提问by Plastus

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).

我想从使用连接到拆分视图控制器的导航控制器呈现的“H”中的视图容器进行切换。我怎样才能做到这一点?我已经尝试过使用本地链接的故事板 ID 进行常规 performSegueWithIdentifier,但这会删除顶部导航栏。我想保留顶部导航栏并执行 segue,就好像它是使用主导航控制器完成的一样(选择在详细视图中显示哪个视图控制器的行)。

Any help is greatly appreciated!

任何帮助是极大的赞赏!

回答by vacawama

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

这是如何从嵌入式 ViewController 执行 segue 的示例。

Sample Storyboard

示例故事板



ViewController.swift

视图控制器.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

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 HViewControllertell its embedding viewController to perform the segue.
  2. Create a protocol called SegueHandlerwhich 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 classdeclaration line:

    class ViewController: UIViewController, SegueHandler {
    

    and by implementing the required function.

  4. Add a delegateproperty 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 showsegue between ViewController and the GreenViewController by Controldragging from the viewController icon at the top of ViewController to the GreenViewController. Name this segue "GoToGreen"in the Attributes Inspector.

  7. In prepareForSeguefor ViewController, when the "EmbedH"segue happens, set the delegateproperty of HViewControllerto self(ViewController).

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

  1. 使用委托来HViewController告诉其嵌入的 viewController 执行 segue。
  2. 创建一个被调用的协议SegueHandler,它只描述一个实现方法的类segueToNext(identifier: String)

    protocol SegueHandler: class {
        func segueToNext(identifier: String)
    }
    
  3. 通过将它添加到class声明行,让你的 viewController 实现这个协议:

    class ViewController: UIViewController, SegueHandler {
    

    并通过实现所需的功能。

  4. 添加一个delegate属性到HViewController

    weak var delegate: SegueHandler?
    
  5. 单击 ViewController 和 HViewController 之间的嵌入转场箭头。"EmbedH"Attributes Inspector 中给它一个标识符。

  6. 通过从 ViewController 顶部的 viewController 图标拖动到GreenViewController,在 ViewController 和 GreenViewController 之间创建一个显示Control转场。"GoToGreen"Attributes Inspector 中命名这个 segue 。

  7. prepareForSegueViewController 中,当"EmbedH"发生 segue 时,将delegate属性设置HViewControllerself(ViewController)。

  8. 当用户单击 中的H按钮时HViewController,调用delegate?.segueToNext("GoToGreen")以触​​发 ViewController 中的 segue。



Here it is running in the simulator:

它在模拟器中运行:

Simulator Demonstration

模拟器演示

回答by FabricioStein

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:

我正是需要@vacawama 在这里提出的内容,虽然我无法重现,但我完全按照您的步骤进行self.delegate?.segueToNext("GoToGreen")了尝试,但被调用了,但既不是协议本身也不是容器视图控制器。经过一整天搜索这种方法后,我意识到问题出在 swift 版本上。只需替换这个:

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.

我遗漏的其他细节是关于嵌入式转场。一定要把容器View 连接到HViewController,而不是View Controller 本身,否则不会出现segue 的Embed 选项。