私は、モーダルに提示されたotherVC内のあるrootVCからの情報を使用したいと思います。ここでの設定は次のとおりです。rootVCとモーダルに提示されたotherVCとの間で情報を転送できませんか?
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
関数を実行すると、オプションの値をアンラップするときに予期せずnilが見つかったため、xcodeが致命的なエラーを検出するという問題がありました。 ButtonAction
の中にprintステートメントを入れると、stringy
がnilではないことがわかります。 otherVC
のラベルは正しく設定されているので、何がnil値を与えているのか分かりません。
はあなたを持っています値を文字列として渡し、AlertVCのviewDidLoadにラベルを設定しようとしましたか?ビューコントローラを表示する前にあなたのラベルがまだnilだと思う。 – mat