FirstViewCOntrollerのボタンアクション時に変数を受け取り、delegateを利用してSecondViewControllerでその値を受け取りたいのですが、うまくいきません。変数はmy_idです。
そのコードは下に示します。
<AppDelegate.swift>
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? var my_id: AnyObject?
〜〜省略〜〜
<FirstViewController>class FirstViewController: UIViewController {
var appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
〜〜省略〜〜
@IBAction func StartButton(_ sender: UIButton) {
〜〜省略〜〜
self.appDelegate.my_id = all_member_array?.count as AnyObject
〜〜省略〜〜
<SecondViewController>class SecondViewController: UIViewController {
var appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
print(self.appDelegate.my_id)
〜〜省略〜〜
SecondViewControllerのprintによる結果はnilで返されてしまい、FirstViewControllerでの変数が受け取れませんでした。FirstViewControllerで代入した変数を受け取れる解決策がありましたらよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー