0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

XcodeをXibファイルで起動する!!(Xcode13.4.1)

Posted at

はじめに

Xcodeでは通常だとStoryboardを使ってアプリを制作しますが、

・Gitでコンフリクトが発生しやすい
・画面遷移が複雑になりやすい

など、デメリットがあります。

そこで、前途したデメリットを補う方法として、起動時xibファイルを使って起動する方法を紹介したいと思います。
他の記事がXcodeのversionが古いものだったりして、色々と試行錯誤した事もあり、備忘録的に記事に残します。

1.Appファイルの作成したら

初期画面は下記の画面になっています。
まずはViewControllerとMainを削除しましょう。
qiita1.png

次にinfoを選択し、Main storyboard file base nameを削除します。
qiita2.png

その次はこちらのinfoからStoryboard Nameを削除します。
qiita3.png

2.xibファイルの作成

⌘ + Nショートカットキーで新規ファイルを作成します。
Cocoa Touch Classで作成して下さい。

スクリーンショット 2022-06-29 5.10.51.png
Subclass of: にはUIViewControllerを選択しましょう。
また、Also create XIB fileにはチェックを入れて下さい!!
スクリーンショット 2022-06-29 5.11.01.png

3.SceneDelegateの設定変更

初期画面を先程作成したViewControllerに設定する為、SceneDelegateにコードを記述していきます。

SceneDelegate
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        
        guard let scene = (scene as? UIWindowScene) else { return }
        self.window = UIWindow(windowScene: scene)
        // NavigationController無しバージョン
//        let viewController = ViewController()
//        self.window?.rootViewController = viewController
//        self.window?.makeKeyAndVisible()
        
        // NavigationController有りバージョン
        let navigationController = UINavigationController(rootViewController: ViewController())
        self.window?.rootViewController = navigationController
        self.window?.makeKeyAndVisible()
    }

以上でビルド出来ます!!
Xcodeのversionでやり方が結構変わるので、他のやり方で出来ない場合、一度この方法で試して貰えたらと思います。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?