Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[220722] TIL #68

Closed
Taehyeon-Kim opened this issue Jul 22, 2022 · 0 comments
Closed

[220722] TIL #68

Taehyeon-Kim opened this issue Jul 22, 2022 · 0 comments
Assignees

Comments

@Taehyeon-Kim
Copy link
Owner

Taehyeon-Kim commented Jul 22, 2022

화면 전환에서 헷갈릴만한 지점

  • 네비게이션 스택 관리 (코드로 화면 전환을 구현할 때 놓치기 쉽다.)
  • 네비게이션 스택으로 관리되다가 중간에 Present된 모달이 나오면 스택이 끊기게 된다.
  • 중간에 새롭게 네비게이션 컨트롤러를 embed해서 전환을 이어나갈 수 있고, 이 시점부터는 새로운 네비게이션 스택이 생기게 된다.
  • 시작점의 중요성 (탭바 컨트롤러, 네비게이션 컨트롤러)
  • 스토리보드에 네비게이션 컨트롤러를 embed 해놓더라도 인스턴스를 가져올 때 주의하지 않으면 원하는 구현을 할 수 없다.

화면 전환하면서 값 전달

  1. 전달 받을 화면에 공간 만들기
  2. 데이터 전달하기
  3. 받은 데이터 가지고 뷰에 표현하기
  4. 화면 전달 시 적절하게 변수에 값을 담아보내기 위해서는 다운 캐스팅이 필요하다. (우리가 ViewController 안에 만들어 놓은 변수에 접근하기 위해서는 당연하게도 타입 캐스팅이 필요하게 된다.)
  5. 아웃렛이 만들어지는 시점이 초기화되는 시점보다 더 나중이기 때문에 그 전에 접근하려고 하면 에러가 발생한다.

에러 체크

  • UIButton의 Style이 plain이면 currentTitle에 접근할 수 없다.

옵셔널 값 체크

var placeholder: String?

self.userTextField.placeholder = placeholder
self.userTextField.placeholder = "\(placeholder ?? "nil value")"

따로 따로 프로퍼티 생성하지 않고, 하나의 구조체 전체를 전달 받는 것이 나은 이유

var movieData: Movie?
  • 데이터 변경(추가, 삭제)등에 대처하기 쉽다.

시작 화면의 변경

앱을 사용할 때마다 로그인/회원가입/인트로 화면이 계속해서 나온다면 사용자 입장에서 많이 불편할 것이다. 따라서 이 부분에 대한 해소가 필요하다.

  1. 로그인 화면
  • 기사용자 : 메인화면
  • 최초 가입자 : 로그인/회원가입 화면
  1. toss
  • 인트로 화면
  • 온보딩 화면
  • 튜토리얼 화면

UIWindow

  • 수많은 화면 중에서 어떤 것을 아이폰 화면에 보여줄 지 관리하는 객체
// 시작 화면을 연결하기 전에 해당 메서드에서 통제
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    guard let scene = (scene as? UIWindowScene) else { return }
    self.window = UIWindow(windowScene: scene)
    
    let sb = UIStoryboard(name: "Trend", bundle: nil)
    guard let vc = sb.instantiateViewController(withIdentifier: "ViewController") as? ViewController else { return }
    
    self.window?.rootViewController = vc
    self.window?.makeKeyAndVisible()
}

시작 화면 분기 처리

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
    guard let scene = (scene as? UIWindowScene) else { return }
    self.window = UIWindow(windowScene: scene)
    
    UserDefaults.standard.set(false, forKey: "First")
    
    // true이면 ViewController, false이면 SearchTableViewController
    if UserDefaults.standard.bool(forKey: "First") {
        let sb = UIStoryboard(name: "Trend", bundle: nil)
        guard let vc = sb.instantiateViewController(withIdentifier: "ViewController") as? ViewController else { return }
        self.window?.rootViewController = vc // 어떤 뷰 컨트롤러를 보여주겠다.
    } else {
        let sb = UIStoryboard(name: "Search", bundle: nil)
        guard let vc = sb.instantiateViewController(withIdentifier: "SearchTableViewController") as? SearchTableViewController else { return }
        self.window?.rootViewController = vc // 어떤 뷰 컨트롤러를 보여주겠다.
    }

    self.window?.makeKeyAndVisible()     // Window가 뷰 컨트롤러를 화면에 비춰주는 역할
}

로그아웃 (첫 화면으로 돌아가고 싶을 때, 처음부터 시작하고 싶을 때)

  1. 첫 화면으로 전환 (기본적인 화면 전환 방식을 사용하면 안 됨 - push/present)
  2. 데이터 초기화 (UserDefault 데이터 초기화)
  • 테마 바꿀 때
  • 사용자가 데이터를 복구했을 때
  • 학생 모드/튜터 모드 -> 사용자 전환 시
@objc func resetButtonClicked() {
    // 시작 화면 초기화
    // iOS 13.0+ SceneDelegate 쓸 때 동작하는 코드
    // 기존에 쌓여있던 화면들은 메모리에서 전부 내려가게 된다.
    // 앱의 상태를 바꿀 때 window를 사용하는 편이다.
    
    let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
    let sceneDelegate = windowScene?.delegate as? SceneDelegate
    
    let sb = UIStoryboard(name: "Trend", bundle: nil)
    guard let vc = sb.instantiateViewController(withIdentifier: "ViewController") as? ViewController else { return }

    sceneDelegate?.window?.rootViewController = vc
    sceneDelegate?.window?.makeKeyAndVisible()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant