我是Swift的初学者,我很难理解使用委托和协议在page_1和page_2之间发送变量的方式。
我有一个2页的应用程序:ViewController1.swift和ViewController2.swift - 第1页(ViewController1.swift)我有一个文本框(TextBoxControl)和一个按钮。 - 第2页(ViewController2.swift)我有一个标签和一个按钮
我想将第1页的文本从第1页发送到第2页,然后在第2页中将其打印出来。
问题是委托总是为零(它在控制台上打印... print(“Delegate is nil”)。)
技术:XCODE版本9.4.1,swift 4
我怎么解决这个问题?
提前致谢。
如果要将数据从ViewController1发送到ViewController2,则只需按照这些步骤操作即可。
步骤1:
在ViewController2中设置一个变量:like
var strValue : String?
第2步:
在按钮操作ViewController1中编写此代码
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let destination = storyboard.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
destination.strValue = yourTextBoxControl.text
self.navigationController?.pushViewController(destination, animated: true)
第3步:
在ViewController2类中,您可以轻松获得以下值:
override func viewDidLoad() {
super.viewDidLoad()
print(strValue)
}
谢谢希望它会帮助你。