1
0

More than 1 year has passed since last update.

Could not cast value of type 'UIViewController'が出た時の対処法 

Last updated at Posted at 2021-08-29

 Could not cast value of type 'UIViewController'の対処

自分がエラーを起こして、画面遷移ができなかった時の原因と対処を記録として残します。

コードはこんな感じでした。

@IBAction func tappedDontHaveAccountBottun(_ sender: Any) {
        let storyBoard = UIStoryboard(name: "Login", bundle: nil)
        let homeViewController = storyBoard.instantiateViewController(identifier: "LoginViewController") as! LoginViewController
        navigationController?.pushViewController(homeViewController, animated: true)
    }

 エラーの原因

このコードでのidentifierのところがエラーになっていました。
エラーから自分は単純に,Custom class, Module, Storyboard IDなどがうち間違っているのかと推測

 エラーの対処

なので、コードとCustom class, Module, Storyboard IDの相違がないように、コピペして再リロードすると改善しました。

この使っているメソッドは、instantiateViewController(identifier:)です。
つまり、このメソッドは識別されたidentifierを持つViewControllerを作成し、Storyboardでのデータを持ってインスタンスを初期化するメソッドなので、homeViewControllerを、インスタンスを初期化したLoginViewControllerとして作成しているのだと思います。(初心者なりの理解ですので間違っているかもしれません。申し訳ないです、、、)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0