gpt4 book ai didi

ios - 如何将数据传回 Swift 中的第一个 View Controller ?

转载 作者:搜寻专家 更新时间:2023-10-31 22:23:46 24 4
gpt4 key购买 nike

我有两个 View Controller ,我有这段代码可以在两个 View 之间导航

ViewController:
func goToSecondViewController() {
let aa = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.navigationController.pushViewController(aa, animated: true)
}

/

SecondViewController:
func goToFirstViewController() {
self.navigationController.popToRootViewControllerAnimated(true)
}

如何在弹出之前将一些数据发送到第一个 View ?

最佳答案

如果 A 是具有以下声明的 View Controller :

class A : UIViewController {
var data: String!
}

然后,只要你有一个 A 的实例,你就可以直接设置数据属性:

let a = A() // assuming you've defined the init method
a.data = "hello"

编辑

如果你想在弹出之前发送数据,你会做这样的事情:

func goToFirstViewController() {
let a = self.navigationController.viewControllers.first as! A
a.data = "data"
self.navigationController.popToRootViewControllerAnimated(true)
}

(上面没有编译)

关于ios - 如何将数据传回 Swift 中的第一个 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25298036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com