问题描述
我想从"H"内的视图容器中进行选择,该视图容器是使用连接到拆分视图控制器"的导航控制器显示的.我该怎么做?我已经尝试使用本地链接的情节提要ID来定期执行performSegueWithIdentifier,但这会删除顶部的导航栏.我想保留顶部导航栏,并像使用主导航控制器(选择详细视图中要显示哪个视图控制器的行)一样执行segue.
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).
任何帮助将不胜感激!
推荐答案
此处是如何从嵌入式ViewController执行序列的示例.
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")
}
}
设置:
- 使用委托让
HViewController
告诉其嵌入的viewController执行segue. -
创建一个名为
SegueHandler
的协议,该协议仅描述实现方法segueToNext(identifier:String)
的类.
- 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)
}
使您的viewController通过将其添加到 class
声明行中来实现此协议:
Make your viewController implement this protocol by adding it to the class
declaration line:
class ViewController: UIViewController, SegueHandler {
并通过实现所需的功能.
and by implementing the required function.
向 HViewController
中添加 delegate
属性:
weak var delegate: SegueHandler?
单击ViewController和HViewController之间的嵌入segue箭头.在 Attributes Inspector 中为其赋予标识符"EmbedH"
.
通过从ViewController顶部的viewController图标拖动到GreenViewController,在ViewController和GreenViewController之间创建 show 序列.在 Attributes Inspector (属性检查器)中将此名称命名为"GoToGreen"
.
Create a show segue between ViewController and the GreenViewController by dragging from the viewController icon at the top of ViewController to the GreenViewController. Name this segue "GoToGreen"
in the Attributes Inspector.
在ViewController的 prepareForSegue
中,当发生"EmbedH"
设置时,设置 HViewController
到 self
(ViewController).
In prepareForSegue
for ViewController, when the "EmbedH"
segue happens, set the delegate
property of HViewController
to self
(ViewController).
当用户单击 HViewController
中的 H
按钮时,调用 delegate?.segueToNext("GoToGreen")
进行触发在ViewController中进行搜索.
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:
这篇关于如何在导航控制器显示的视图内从容器视图执行segue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!