iOS/ios 아무거나 만들어보기

view 코드로 작성하기(ios programmatic ui)

728x90

App을 생성하면 자동적으로 AppDelegate,SceneDelegate.swift 파일이 생성된다.

SceneDelegate.swift 파일에 아래 코드를 추가하면 StoryBoard말고 코드로 뷰를 시작 할 수 있다.

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let scene = (scene as? UIWindowScene) else { return }
    window = UIWindow(windowScene: scene)
    // 처음 시작하고자 하는 ViewController
    window?.rootViewController = UINavigationController(rootViewController: ViewController())
    window?.makeKeyAndVisible()
}

 

iOS 13부터 SceneDelegate.swift 파일이 추가되었는데 두 파일이 하는 역할이 무엇인지 궁금하다.

 

AppDelegate - application의 entry point 역할을 함. url 열기, life-cycle 관리도 존재한다고 하지만 entry point 역할을 한다고 생각해야겠다. 그래서 앱 실행 시 동작해야하는 행위를 작성함.

SceneDelegate - 어떤 화면을 보여줄지 관리하는 역할 

 

왜 SceneDelegate에서 rootViewController를 지정해주나 했는데 Main.Storyboard를 사용하지 않으면 어떤 화면을 보여줘야하는지 직접적으로 설정하는 곳이였다.

 

아래 다른 블로그에서 자세히 설명해놓았다.

 

[iOS] SceneDelegate & AppDelegate의 역할

* 해당 글에는 개인 견해가 들어가있으므로 이곳을 참고하시면 조금 더 깔끔한 설명을 보실수도(?) 있습니다. 이 글은 SceneDelegate과 AppDelegate의 역할에 대해 다루겠습니다. 공식문서와 DW 블로그

sueaty.tistory.com

반응형