gpt4 book ai didi

ios - 快速在选项卡式 View 之间传递数据

转载 作者:行者123 更新时间:2023-11-30 14:09:23 24 4
gpt4 key购买 nike

我有一个项目,它有一个选项卡式栏 Controller ,该 Controller 链接到 FirstViewController.swfit 和 SecondViewController.swift。

我在第一个 View 中有一个文本框,在第二个 View 中有一个标签和一个按钮。我希望用户能够在第一个 View 的文本框中输入内容,然后切换到第二个选项卡,然后按下按钮,让他们在文本框中输入的内容出现在标签中。

我尝试过到处研究,并且遇到了 Segues 和 Singleton,但我不确定如何准确应用它们,或者它们对于我想要实现的目标是否是必要的。

谢谢!

编辑

我的 SecondViewController.swift 代码基于 razor 的回复:

import UIKit

class SecondViewController: UIViewController {

var firstViewController: FirstViewController?

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBOutlet weak var label: UILabel!

@IBAction func button(sender: AnyObject) {

if let fvc = firstViewController {
self.label.text = fvc.textbox.text
}

}
}

最佳答案

常见的做法是让 SecondViewController 引用 FirstViewcontroller。因此,在 ViewController 类中,您将有例如可选变量,例如:

var firstViewController: FirstViewController?

然后,当您处理按钮按下时,您可以执行类似的操作

if let fvc = firstViewController {
self.label.text = fvc.textbox.text
}

关于ios - 快速在选项卡式 View 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31923743/

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