[英]How to assign value to an outlet after delegating it?
所以我的应用程序有 3 个屏幕:Main、Second 和 Result(非常简单)
在主屏幕上,我显示一个 label 和按钮来改变它
在第二个中,我使用 textinput 更改 label 并将其传递给 Result(有
导航控制器)
在最后一个屏幕上,我显示结果和 2 个按钮:保存和取消
我的问题是我不能为 Main 的插座赋值,因为它是 nil,我不能对 viewDidLoad() 做任何事情,因为它只在应用程序启动时工作一次。
我能做些什么来解决这个问题? 是否有任何 function 可以重新加载视图,以便我可以在 viewDidLoad 中分配值?
整个应用程序在这里: https://drive.google.com/file/d/1mvL2fVxjOHbL4dReCwJ8poIq9G9-ezny/view
主VC:
class MainVC: UIViewController, ResultVCDelegate {
func passData(text: String) {
// label.text = text -- throws error
}
@IBOutlet weak var label: UILabel!
@IBAction func change(_ sender: Any) {
let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NavVC")
present(nextVC, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
第二个VC:
class SecondVC: UIViewController {
@IBOutlet weak var inputText: UITextField!
@IBAction func save(_ sender: Any) {
let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ResultVC") as! ResultVC
nextVC.labelText = inputText.text!
navigationController?.pushViewController(nextVC, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
结果VC:
protocol ResultVCDelegate {
func passData(text: String)
}
class ResultVC: UIViewController {
var delegate: ResultVCDelegate?
var labelText = ""
@IBOutlet weak var label: UILabel!
@IBAction func saveAndGoHome(_ sender: Any) {
let mainVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MainVC") as! MainVC
self.delegate = mainVC
delegate?.passData(text: labelText)
dismiss(animated: true, completion: nil)
}
@IBAction func cancel(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
label.text = labelText.isEmpty ? label.text : labelText
}
}
顺便说一句:我用两个屏幕做了类似的应用程序,它就像一个魅力......奇怪
-> Main VC(你需要获取数据的地方)
class MainVC: UIViewController, ResultVCDelegate {
func passData(text: String) {
// label.text = text -- throws error
print(text)
}
@IBOutlet weak var label: UILabel!
@IBAction func change(_ sender: Any) {
let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NavVC")
nextVC.delegate = self
present(nextVC, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
-> Result VC(从你需要发送数据的地方)
protocol ResultVCDelegate: AnyObject {
func passData(text: String)
}
class ResultVC: UIViewController {
weak var delegate: ResultVCDelegate?
var labelText = "Please Enter Something"
@IBOutlet weak var label: UILabel!
@IBAction func saveAndGoHome(_ sender: Any) {
self.delegate?.passData(text: labelText)
dismiss(animated: true, completion: nil)
}
@IBAction func cancel(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
label.text = labelText.isEmpty ? label.text : labelText
}
}
继续我的评论。
查看您的代码,问题在于您没有将 MainVC 的引用传递给 ResultVC,而是创建了 MainVC 的新实例,这会导致崩溃,因为视图 controller 尚未正确创建。 但是您不想创建新实例,因为您需要对您创建的原始 MainVC 的引用。
您可以通过传递引用来获得此信息,您需要更新所有三个 ViewController。 像这样的东西应该工作。 注意我没有对此进行测试,但这是一般原则。
class MainVC: UIViewController, ResultVCDelegate {
func passData(text: String) {
label.text = text
}
@IBOutlet weak var label: UILabel!
@IBAction func change(_ sender: Any) {
let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NavVC")
nextVC.delegate = self // Here we add the delegate (the reference to MainVC)
present(nextVC, animated: true, completion: nil)
}
}
我们需要在此处添加委托,然后将其转发到 ResultVC。
class SecondVC: UIViewController {
weak var delegate: ResultVCDelegate? // Add the delegate that will hold a reference to MainVC
@IBOutlet weak var inputText: UITextField!
@IBAction func save(_ sender: Any) {
let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ResultVC") as! ResultVC
nextVC.labelText = inputText.text!
nextVC.delegate = delegate // Here we pass the reference to MainVC
navigationController?.pushViewController(nextVC, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
我们可以从 storyboard 中删除用于实例化 MainVC 的代码,而只需使用委托将值传递回 MainVC。
class ResultVC: UIViewController {
weak var delegate: ResultVCDelegate?
var labelText = ""
@IBOutlet weak var label: UILabel!
@IBAction func saveAndGoHome(_ sender: Any) {
// Just use the delegate no need to create a new instance
delegate?.passData(text: labelText)
dismiss(animated: true, completion: nil)
}
@IBAction func cancel(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
label.text = labelText.isEmpty ? label.text : labelText
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.