[TIL] rootViewController 설정하기

한철희·2024년 5월 15일
0

TIL

목록 보기
43/57

여러분들은 Main 스토리보드 이외의 페이지를 작업할 때
어떤 방식으로 시뮬레이터에서 확인하시나요?

아마 info.plist와 프로젝트 Targets에서 설정을 건드리는
방법을 알고 계실거같습니다

저는 이 방법 대신에 SceneDelegate의 코드를 수정하는 방법을 쓰는데요
이렇게 하면 협업시 머지할 때 미처 변경하지 못한 설정때문에 컨플릭을 마주할 일도 없고 코드 몇 줄만 추가하는 간단한 방법이여서 선호합니다

스토리보드와 코드베이스 두 가지 방법이 있으니 필요한 부분을 봐주세요!


스토리보드 방식

스토리보드 방식이 좀 더 간단합니다! 아래의 코드 한 줄만 추가해주시면 됩니다

window!.rootViewController = UIStoryboard(name: "YourPage", bundle: nil).instantiateInitialViewController()!

실제로 추가하면 아래와 같은 코드가 되겠네요

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
      window!.rootViewController = UIStoryboard(name: "YourPage", bundle: nil).instantiateInitialViewController()!
        guard let _ = (scene as? UIWindowScene) else { return }
    }

코드베이스 방식

코드베이스의 경우 스토리보드보다 조금 더 코드가 있습니다
그렇다고 어렵진 않으니까요 보시고 차근차근 적용해보시면 될겁니다!

window = UIWindow(windowScene: windowScene)
window?.rootViewController = ViewController() // 원하는 뷰컨트롤러로 변경해주기
window?.makeKeyAndVisible()

위의 3줄 코드를 추가해주시면 됩니다~
실제로 적용해보면 아래와 같은 모양이 될겁니다!

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }
    
    window = UIWindow(windowScene: windowScene)
    window?.rootViewController = ViewController() // 원하는 뷰컨트롤러로 변경해주기
    window?.makeKeyAndVisible()
}

profile
초보 개발자 살아남기

0개의 댓글