[iOS] Code Based 초기 세팅하기

어흥·2024년 6월 29일
0

iOS

목록 보기
4/10

Codebased로 혼자 간단한 플젝을 진행하려고 하는데 codebased 해본 적이 없어 처음이라 엄청 설렌다 ㅎㅎ

codebased하려면 3가지 초기세팅이 필요한데, 잊지 않기 위해서 기록하려고 한다.

1. Build Settings 수정하기

Build Settings에서 'UlKit Main Storyboard File Base Name'가 Main으로 설정되어 있는데, 이걸 지워야 한다.

변경 전

⬇️

변경 후

2. Info.plist 수정

Info.plist에서 'Stroryboard Name' value를 Main으로 설정되어 있는데 이 또한 지워야 한다.

변경 전

⬇️
변경 후

3. SceneDelegate.swift 파일 수정

SceneDelegate 파일의 scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) 함수 내부를 수정해야한다.

변경 전

//  SceneDelegate.swift
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
	func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else { return }
    }
    ...
 }

⬇️
변경 후

func scene(_ scene: UIScene,willConnectTo session: UISceneSession,options connectionOptions: UIScene.ConnectionOptions){
        guard let windowScene = (scene as? UIWindowScene) else { return }
        let window = UIWindow(windowScene: windowScene)
        let mainViewController = MainViewController()
        
        let navigationController = UINavigationController(rootViewController: mainViewController)
        window.rootViewController = navigationController
        
        self.window = window
        window.makeKeyAndVisible()
    }
  • 첫 화면으로 정하고 싶은 ViewController 인스턴스를 생성하고 해당 인스턴스를 windowrootViewController에 할당한다.

나는 NavigationController를 사용할 것이므로 NavigationController 인스턴스를 rootViewController로 설정하였다.

0개의 댓글