Help us understand the problem. What is going on with this article?

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "xxx" nib but the view outlet was not set.'というエラーがでたとき。

概要

イニシャライザでnibを呼び出す時に次のようなエラーが出た。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "xxxViewController" nib but the view outlet was not set.'
init()
init() {
        super.init(nibName: UserSearchViewController.className, bundle: nil)
    }

loadViewで呼び出した時はこのエラーがでなかった。

loadView()
override func loadView() {
        guard let view = UINib(nibName: "UserSearchViewController", bundle: nil).instantiate(withOwner: self, options: nil).first as? UIView else { return }
        self.view = view
    }

結論

貼り付けた画像_2020_03_01_13_57.png

  • ①InspectorのFile`sOwnerにCustomClassをセットする。
  • ②Placeholders>File`s OwnerからViewにOutletで接続する。(※画像を参考)

これでエラーがでなくなりました。

コードで実装していたときは、Ownerを指定したのに、init()の時はnibをロードするだけでOwnerを指定していたなかったのが原因かと思われます。

参考

https://qiita.com/maru_u/items/9b2f7dc0c4f5f3f5cac8

snt-nbzw
iOS Developer / Swift / Objective-C
https://hyaku-juu-ichi.hatenablog.com/
caraquri
ネイティブモバイルアプリの開発を強みとする港区の開発会社。
https://caraquri.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした