gpt4 book ai didi

ios - SWIFT 从另一个 Controller 访问数据

转载 作者:行者123 更新时间:2023-11-30 13:34:23 27 4
gpt4 key购买 nike

例如,我有 2 个 Controller 已在内存中(Class 1、Class 2)。如何从 2 类访问 1 类数据?

class Class_1: UIViewController {

var number:UInt8 = 1

override func viewDidAppear(animated: Bool) {
number = 8
}
}

如何访问 2 类中的数字变量并打印它的值?重点是不要创建新实例,重点是获取内存中 Class 1 的指针并访问它的数据。

最佳答案

首先将 ViewController 的名称从 Class_1 更改为 Class1ViewController,将 Class_2 更改为 Class2ViewController

您需要在Class1ViewController中初始化Class2ViewController时设置变量,并将数据从Class2ViewController传回给 Class1ViewController你需要使用委托(delegate)

要将Class1ViewController数据传输到Class2ViewController,请打开Class1ViewController文件并添加以下prepareForSegue方法(如果您使用 Storyboard)

class Class1ViewController: UIViewController{
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Class2ViewController" {
if let class2ViewController = segue.destinationViewController as? Class2ViewController {
class2ViewController.inforFromClass1 = "Class 2 Variable set from Class 1"
}
}
}
}

但是,如果您使用xib并且有一个从Class1ViewController移动到Class2ViewController的按钮,则应在触发Class2ViewController的按钮的IBAction中编写以下代码

class Class1ViewController: UIViewController{
@IBAction showClass2ViewController(){
let secondViewController = SecondViewController(nibName:"SecondViewController", bundle: NSBundle.mainBundle())
secondViewController.infoFromClass1 = "Class 2 Variable set from Class 1"
self.showViewController(secondViewController, sender: self)
}
}

这是在 Class2ViewController 上设置变量,同时从 Class1ViewController 显示变量的方法

现在要将消息从 Class2ViewController 传递到 Class1ViewController,您需要使用委托(delegate)。打开Class2ViewController并在顶部添加以下协议(protocol)

@objc protocol Class2ViewContollerDelegate :class{
func printMessageFromClass2ViewController()
}

class Class2ViewController: UIViewController {
}

Class2ViewController 类中添加对 delagate 的弱引用,并在其 ViewDidAppear 或您喜欢的任何其他方法中调用它,

@objc protocol Class2ViewContollerDelegate :class{
func printMessageFromClass2ViewController()
}

class Class2ViewController: UIViewController {
weak var delegate: Class2ViewControllerDelegate?
override func viewDidAppear(animated: Bool) {
self.delegate?.printValueFromClass2ViewController
}
}

现在我们已经在Class2ViewController中定义了协议(protocol),我们需要在Class1ViewController中实现它。返回在您的 Class1ViewController 文件中实现这样的协议(protocol)

class Class1ViewController: UIViewController, Class2ViewControllerDelegate {
func printMessageFromClass2ViewController(){
print("hey I just printed message in Class1ViewController through its delegate in Class2ViewController")
}

// For Xibs
@IBAction showClass2ViewController(){
let secondViewController = SecondViewController(nibName: "SecondViewController", bundle: NSBundle.mainBundle())
secondViewController.infoFromClass1 = "Class 2 Variable set from Class 1"
self.showViewController(secondViewController, sender: self)
}

// For storyboards
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Class2ViewController" {
if let class2ViewController = segue.destinationViewController as?Class2ViewController {
class2ViewController.inforFromClass1 = "Class 2 Variable set from Class 1"
}
}
}
}

关于ios - SWIFT 从另一个 Controller 访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36245859/

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