所以我的應用程式有 3 個螢屏:主螢屏、第二螢屏和結果(非常簡單)
在主螢屏上,我顯示了一個標簽和按鈕來更改它
在第二個我用 textinput 更改標簽并將其傳遞給 Result (有
導航控制器)
在最后一個螢屏上,我顯示結果和 2 個按鈕:保存和取消
我的問題是我無法為 Main 的 outlet 賦值,因為它為零,而且我無法對 viewDidLoad() 做任何事情,因為它只在應用程式啟動時作業一次。
我能做些什么來解決這個問題?是否有任何功能可以重新加載視圖,以便我可以在 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
}
}
順便說一句:我用兩個螢屏做了類似的應用程式,它就像一個魅力......奇怪
uj5u.com熱心網友回復:
繼我的評論之后。
查看您的代碼,問題在于您沒有將 MainVC 的參考傳遞給 ResultVC,而是創建了 MainVC 的新實體,這會導致崩潰,因為未正確創建視圖控制器。但是您不想創建新實體,因為您需要對您創建的原始 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()
}
}
我們可以從情節提要中洗掉用于實體化 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
}
}
uj5u.com熱心網友回復:
-> 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()
}
}
-> 結果 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
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/395126.html
上一篇:從多個視圖控制器訪問資料/功能
