1. Bindable
  2. LoginControllerDelegate
  • didSet, closure
  • protocol, delegate

Bindable

Bindable은 reactive programming을 위해 필요하다.

class Bindable<T> {
    var value: T? {
        didSet {
            observer?(value)
        }
    }
    
    var observer: ((T?)->())?
    
    func bind(observer: @escaping (T?) ->()) {
        self.observer = observer
    }
    
}

didSet과 observer 클로저 개념을 사용하여 reactive programming을 할 수 있다. Bindable을 사용하지 않아도 구현할 수 있지만 Bindable class를 사용하면 더 깔끔하게 구현할 수 있다.

Bindable.bind로 observer를 선언해줄 때, retain cycle을 방지하기 위해서 [unowned self]를 항상 선언해줘야한다.

LoginControllerDelegate

A protocol is something like things that you have to do. it is usually compiled with delegate. So we clarify functions in protocol for what to do!

protocol LoginControllerDelegate {
    func didFinishLogginIn()
}

class LoginController: UIViewController {
    
    var delegate: LoginControllerDelegate?
    ...
}

Two delegate which type LoginControllerDelegate are declared in LoginController, RegistrationController. When we presented to RegistrationController from HomeController, We set the delegate of RegistrationController is self in HomeController. It means delegate is on HomeController now. So when we push a LoginController on RegistrationController(UINavigationController), we should declare loginController’s delegate is registrationController’s delegate

    @objc fileprivate func handleToLogin() {
        let loginController = LoginController()
        loginController.delegate = delegate
        navigationController?.pushViewController(loginController, animated: true)
    }

in HomeController

inherit LoginControllerDelegate

class HomeController: UIViewController, SettingsControllerDelegate, LoginControllerDelegate, CardViewDelegate {

implement didFinishLogginIn() as you want

    func didFinishLogginIn() {
        fetchUsersFromFirestore()
    }

태그: ,

카테고리:

업데이트:

댓글남기기