LoginSignup
2
5

More than 3 years have passed since last update.

xibで(から)起動する(Storyboardではなく)[Swift]

Last updated at Posted at 2019-08-17

Storyboardとxib

Storyboardでの開発は人によって流派が分かれており
・1Storyboardに1画面
・1Storyboardに複数画面UITabControllerとかに使われがち?

Storyboardじゃなくてxibでよくね?
conflictが起きにくいから
なんてのもあるんだとか。

やり方

xibから起動させる際のやり方と注意点をまとめました

AppDelegateをいじる

hogViewController.xib , hogViewController.swiftの場合を書きます

AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//1: この4行か(navigationControllerつけたい場合はこちら)
        self.window = UIWindow(frame: UIScreen.main.bounds)
        let navigationController =  UINavigationController(rootViewController: hogeViewController())
        self.window?.rootViewController = navigationController
        self.window?.makeKeyAndVisible() //signal SIGABRT, Console(libc++abi.dylib: terminating with uncaught exception of type NSException)
//2: この3行を追加します
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = hogeViewController(nibName: "hogeViewController", bundle: nil)
        window?.makeKeyAndVisible()

        return true
    }

xib側の設定

viewIBOutletみたいな方式で繋ぎます(ただし左クリック)
Placeholders
スクリーンショット 2019-08-18 0.59.27.png

これやらないとAppDelegateのclassの行のところでsignal SIGABRTでクラッシュします

xcode側の設定(注意点)

プロジェクト名.xcodeprojTARGETSDeployment InfoMain Interfaceまで移動します
ここが僕がつまづいたところなんですが、自作のStoryboard作成した時と同じように
気を利かせてhogeViewController.xibを選択するとAppDelegateのclassの行のところでsignal SIGABRTでクラッシュします
(一応選択できてしまうのが落とし穴すぎる。。。w)
スクリーンショット 2019-08-18 0.37.15.png

以上の3点をクリアしてビルドするとこんなふうにちゃんとできてると思います(画像はnavigationControllerの方を書いた場合です)
Storyboardxibが他にあったとしてもこのやり方でいけました
Simulator Screen Shot - iPhone 7 - 2019-08-18 at 02.28.38.png

お疲れ様でした

参考

【Swift4】アプリの初回起動画面を storyboard ではなく xib にする - 3時のKAIHATSU

2
5
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
2
5