我想使用來自模態展示的其他VC中的一個rootVC的信息。下面是它的設置:無法在rootVC和模態呈現的其他VC之間傳輸信息?
protocol OtherVCDelegate {
func didHitOK()
func didHitCancel()
}
class ViewController: UIViewController, OtherVCDelegate {
func didHitCancel() {
//do a function
}
func didHitOK() {
//do another function
}
var stringy = "Hello"
@IBAction func ButtonAction(_ sender: Any) {
let otherVC = self.storyboard?.instantiateViewController(withIdentifier: "AlertVC") as! AlertVC
otherVC.modalPresentationStyle = .overCurrentContext
otherVC.delegate = self
otherVC.label.text = stringy //THIS is where my question focuses
self.present(otherVC, animated: true, completion: nil)//presents the other VC modally
}
的otherVC
有一個名爲「標籤」的UILabel。但是,我遇到的問題是,運行ButtonAction
函數時,xcode發現一個致命錯誤,因爲它在解包可選值時意外發現爲零。我有點困惑,爲什麼會發生這種情況,因爲在ButtonAction
內輸入打印語句證實stringy
不是零。 otherVC
中的標籤正確設置,因此我不確定什麼是零值。
有你試圖將該值作爲字符串傳遞,並在AlertVC中的viewDidLoad上設置標籤?我認爲在您呈現視圖控制器之前,您的標籤仍然是零。 – mat