swift - 如何使用prepare for segue发送结构体实例

标签 swift struct segue

假设我有

struct cat {
var paws: int
var name: string
var breed: string

}

我如何将实例连接到新的目标 Controller ?特别是这个实例从数组到新的 DC?

prepare for segue

{
if segue.identifier == "segue"
var nextVC = segue.desitnationviewcontroller as ...
nextvc.instance = ?

}

最佳答案

你可以像这样分配它:

let someCat = cat(paws: 4, name: "Kitty", breed: "Unknown")
let arrayCat = [cat(paws: 5, name: "Mutant", breed: "Unknown"),
                cat(paws: 4, name: "John", breed: "Doe")]

var nextVC = segue.desitnationviewcontroller as SomeViewController
nextVC.somePropertyName = someCat // or arrayCat if you're using an array

在您的 SomeViewController 中,您必须拥有一个类型为 cat 的属性,并且您可以直接分配它。例如:

class SomeViewController: UIViewController {
    var somePropertyName: cat? // [cat]() if it's an array of type cat
}

此外,为了您的方便,我添加了 Apple Swift 文档的链接。链接为here .

关于swift - 如何使用prepare for segue发送结构体实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29896791/

相关文章:

ios - 如何根据具有自动调整大小的传入数据在 Collection View 单元格中添加随机内容(UIKit 元素)?

ios - 从应用程序的 View 中删除 map 时出现奇怪的错误

variables - 如何从另一个函数访问结构变量?

c# - 在 C# 中使用接口(interface)时的值与引用类型

ios - 从一个 View Controller 选择的多个数组元素到另一个 View Controller

ios - 如何知道 NSData 是否完成下载以触发方法

ios - 在 UITextField 的开头创建空间

C 函数不返回列表值

ios - 使用我的 ViewController 作为 TabBarController

ios - 无法在初始加载时以编程方式设置 UINavigationItem 标题