멀고도 험난한 개발 일지

UIKit_유투브_신동규 - #1 Pro처럼 project 시작하기

이드entity 2022. 7. 10. 17:09
반응형

 

목표: ViewController.swift, Main.storyboard 파일을 직접 사용하진 않을 것

 

 

UIKit

UIKit란? 화면을 구성하는 것들을 해줄 수 있는 라이브러리 (폰트, 라벨 등등)

 

 


 

 

처음에 기본이 되는 UIViewController 삭제함

그리고 새로 .swift 파일 생성 후 ViewController 선언!

import Foundation
import UIKit

class RootViewController: UIViewController {

}

UIViewController을 상속함으로써 UIViewController 역할을 할 수 있게 하자


·

·

·

 

LifeCycle(생성자) 선언

class RootViewController: UIViewController {
    // MARK: Life Cycle functions
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

View가 load 되었을 때 이 코드부터 실행하게 된다

하지만, 이 파일이 사용되려면 SceneDelegate 바꾸어야 함


·

·

·

 

새로 생성한 ViewController 사용하기 위해 SceneDelegate 편집

var window: UIWindow?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let scene = (scene as? UIWindowScene) else { return }

    window = UIWindow(windowScene: scene)
    window?.rootViewController = RootViewController()
    window?.makeKeyAndVisible()
}

window 인스턴스 기본으로는 nil 값이지만, 새롭게 인스턴스 생성하고

window의 rootViewController를 위에서 생성한 RootViewController로 설정!

makeKeyAndVisible로 해당 window를 보여주고, key window로 지정하면 RootViewController()의 내용물들이 나옴!!

반응형