appDelegateからHomeViewControllerのメソッドhello()を呼び出そうとしたところ
こちらのエラーが表示されアプリが落ちてしまいます。
Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
どこが問題なのでしょうか?
AppDelegate
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var homeViewController: HomeViewController! func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.homeViewController.hello()//ここでエラー表示される落ちる return true } }
HomeViewController
import UIKit class HomeViewController: UIViewController { let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate override func viewDidLoad() { super.viewDidLoad() appDelegate.homeViewController = self print("home") } func hello() { print("hello") } }