なんとかするから、なんとかなる

エンジニア関係のことを書きます

Xcode「...nib but the view outlet was not set」というエラーが出たとき

背景

xibファイルを利用してCustomViewを作成して、ViewControllerと接続したときに遭遇した問題です。

解決方法

  1. File's OwnerのCustomClassを設定する

  2. OutletsのViewとviewを接続する

Interface Builderへ行き、~~ViewController.xibのFile's Ownerの上でControlを押しながらドラッグ。

Viewの背景(UIView)の上で離す。

これでFile's OwnerとUIViewの関連づけが行われる。

Tips

xibファイルにViewControllerはおいてはいけない。

そのかわりFile's Ownerがあるので、そこでViewControllerを指定する。

したがって、IBOutletやIBActionは基本的にFile's Ownerに対して接続する。

呼び出しかたの一例

let modal = MyViewController(nibName: "NIB FILE NAME", bundle: nil)
self.present(modal, animated: true, completion: nil)