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
How to perform segue from container view within a view displayed by navigation controller?
提问by Plastus
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 的示例。
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:
设置:
- Use delegation to have the
HViewController
tell its embedding viewController to perform the segue. Create a protocol called
SegueHandler
which just describes a class that implements the methodsegueToNext(identifier: String)
.protocol SegueHandler: class { func segueToNext(identifier: String) }
Make your viewController implement this protocol by adding it to the
class
declaration line:class ViewController: UIViewController, SegueHandler {
and by implementing the required function.
Add a
delegate
property toHViewController
:weak var delegate: SegueHandler?
Click on the embed segue arrow between ViewController and HViewController. Give it the identifier
"EmbedH"
in the Attributes Inspector.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.In
prepareForSegue
for ViewController, when the"EmbedH"
segue happens, set thedelegate
property ofHViewController
toself
(ViewController).When the user clicks the
H
button in theHViewController
, calldelegate?.segueToNext("GoToGreen")
to trigger the segue in ViewController.
- 使用委托来
HViewController
告诉其嵌入的 viewController 执行 segue。 创建一个被调用的协议
SegueHandler
,它只描述一个实现方法的类segueToNext(identifier: String)
。protocol SegueHandler: class { func segueToNext(identifier: String) }
通过将它添加到
class
声明行,让你的 viewController 实现这个协议:class ViewController: UIViewController, SegueHandler {
并通过实现所需的功能。
添加一个
delegate
属性到HViewController
:weak var delegate: SegueHandler?
单击 ViewController 和 HViewController 之间的嵌入转场箭头。
"EmbedH"
在Attributes Inspector 中给它一个标识符。通过从 ViewController 顶部的 viewController 图标拖动到GreenViewController,在 ViewController 和 GreenViewController 之间创建一个显示Control转场。
"GoToGreen"
在Attributes Inspector 中命名这个 segue 。在
prepareForSegue
ViewController 中,当"EmbedH"
发生 segue 时,将delegate
属性设置HViewController
为self
(ViewController)。当用户单击 中的
H
按钮时HViewController
,调用delegate?.segueToNext("GoToGreen")
以触发 ViewController 中的 segue。
Here it is running in the simulator:
它在模拟器中运行:
回答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 选项。