Swiftでアプリケーションを作成しています。
Swiftのバージョンは4.1
xcodeは9.3を使用しています。
私はボタンを押すと同一の画面上にあるUIViewの中にxibで作成したビューを表示させたいと考えています。
しかし、xibで作成したビューを表示させようとしている時に、以下のエラーが発生しました。
Fatal error: Unexpectedly found nil while unwrapping an Optional value
調べてみるとFile's Ownerに設定してあるクラスで読み込むUIViewが読み込めていないことがわかりました。
import UIKit class CustomDialog: NSObject { private static let className = "CustomDialog" @IBOutlet weak var customView: UIView! // これがnil? override init() { super.init() Bundle.main.loadNibNamed(CustomDialog.className, owner: self, options: nil) } func show(presentedView: UIView?) { if let unwrappedPresentedView = presentedView { print(customView) // none unwrappedPresentedView.addSubview(customView) } else { fatalError("View is Nil!") } } }
また、ViewControllerは以下の通りです。
import UIKit class ViewController: UIViewController{ @IBOutlet weak var sampleView: UIView? var customDialog: CustomDialog = CustomDialog() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func buttonTouch(_ sender: Any) { customDialog.show(presentedView: self.sampleView) } }
これを実現するにあたり、参考にさせていただいているサイトはこちらになります。
https://qiita.com/negi0205/items/65cca71229dea0fc2b59
私自身経験がとても浅く、解決の目処も立たないため、皆様のお力をお借りできればと思い、質問させていただきました。
しかし初めての質問なため、情報が不足していると思いますので、不明な点はお答えいたします。
何卒よろしくお願いします
回答1件
あなたの回答
tips
プレビュー