我想在模态呈现的其他VC中使用来自一个rootVC的信息。以下是它的设置:
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
有一个名为“label”的UILabel。但是,在运行ButtonAction
函数时遇到的问题是xcode发现致命错误,因为在解包可选值时它意外地发现了nil。我对为什么会发生这种情况感到有点困惑,因为在ButtonAction
内放入一个打印语句证实stringy
不是零。 otherVC
中的标签是'正确设置',所以我不确定是什么给出了零值。
答案 0 :(得分:1)
在您呈现视图控制器之前,我认为您的标签无法使用。改为传递字符串,并在main()
...
% this works
fplot('4 * (x^-12 - x^-6)', [0.98,2]);
% these and other combinations don't work
fplot('potential', [0.98,2]);
fplot('potential(x)', [0.98,2]);
plot(0.98:0.01:2, potential(x));
end
function v = potential(x)
v = 4 * (x^-12 - x^-6);
end
AlertVC
方法中设置标签文字。
在你的viewDidLoad
声明一个字符串:
AlertVC
然后更改您的代码
var stringy:String?
此时您可以在 @IBAction func ButtonAction(_ sender: Any) {
let otherVC = self.storyboard?.instantiateViewController(withIdentifier: "AlertVC") as! AlertVC
otherVC.modalPresentationStyle = .overCurrentContext
otherVC.delegate = self
otherVC.stringy = stringy //you pass the string instead of setting the label text
self.present(otherVC, animated: true, completion: nil)//presents the other VC modally
}
:
viewDidLoad