관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - swift] 4-2) RIBs 튜토리얼 (Listener Interface) 본문

Architecture (swift)/RIBs

[iOS - swift] 4-2) RIBs 튜토리얼 (Listener Interface)

jake-kim 2021. 4. 20. 00:30

튜토리얼 RIB 트리

  • 현재 LoggedOut RIB 구현 완료
  • 새로 만들어질 LoggedIn RIB은 View가 없는 RIB
  • 플로우: LoggedIn에서 로그인이 성공 > OffGame에서 start button 탭 -> TicTacToe RIB의 뷰 표출

Child -> parent 데이터 넘기기 (Listener Interface)

  • 내용: LoggedOutViewController에서 플레이어 1, 2이름을 입력 후 로그인
      -> LoggedInRIB을 통해 TicTacToe 게임 ViewController 화면이 유저에게 표출
  • LoggedOut RIB이 Root RIB에게 로그인 완료 상태 알림
    • LoggedOutViewController에서 유저가 Login 탭 -> LoggedOutInteractor가 비즈니스 로직 처리
    • Login이 성공하면 LoggedOutInteractor가 Root RIB에게, 로그인 성공 + LoggedIn RIB으로 이동 알림
      (LoggedInteractor에서 RootRouter와 연결되어 있는 LoggedOutListener protocol 이용)
// LoggedOutInteractor

protocol LoggedOutListener: class {
    func didLogin(player1Name: String, player2Name: String)
}

final class LoggedOutInteractor (...) {
    ...
    
    func login(withPlayer1Name player1Name: String?, player2Name: String?) {
        let player1NameWithDefault = playerName(player1Name, withDefaultName: "Player 1")
        let player2NameWithDefault = playerName(player2Name, withDefaultName: "Player 2")

        listener?.didLogin(player1Name: player1NameWithDefault, player2Name: player2NameWithDefault)
    }
}
  • 위에서 LoggedOutListener에 함수를 추가하면, RootInteractable에서 LoggedOutListener를 구현 강제

// RootInteractor.swift

final class RootInteractor (...) {
    ...
    
    func didLogin(player1Name: String, player2Name: String) {
        print("\(player1Name) vs \(player2Name)")
    }
}

 

* 참고

github.com/uber/RIBs/wiki/iOS-Tutorial-2

Comments