iOS(Swift) - UINavigationController,Coordinator,Protocol-[MVVM Design pattern](1)


こんにちは.
今日紹介する内容は~~
ストーリーボードを使用せずにrootView Controlを設定!
設定したViewControllerをシミュレータにアップロードしてみます!

プロジェクトの作成後!->Coordinator SWIFTファイルを作成してください
ここでViewControllerを移動しましょう.
まず和弦を見てから説明しましょう.
import UIKit

protocol CoordinatorProtocol {
    var childCoordinator : [CoordinatorProtocol] { get }
    func goingStart()
}

// 파이널은 마음대루!!
final class Coordinator : CoordinatorProtocol{
    
    private(set) var childCoordinator: [CoordinatorProtocol] = []
    
    private let window : UIWindow
    
    init(window:UIWindow) {
        
        self.window = window
    }
    
    func goingStart() {
        let navigationController = UINavigationController()
        
        let dongGangAjiCoordinator = DongGangAjiCoordi(navigationController: navigationController)
        
        childCoordinator.append(dongGangAjiCoordinator)
        
        dongGangAjiCoordinator.goingStart()
        
        window.rootViewController = navigationController
        
        window.makeKeyAndVisible()
    }
}
}
一つ一つ説明していきましょう!
まずはプロトコルでデータを伝えます!!
protocol CoordinatorProtocol {
var childCoordinator : [CoordinatorProtocol] { get }
func goingStart()
}
私はこのプロトコルでデータを伝えます!!
SWIFTはプロトコル向けのプログラミングなので、とても重要な部分です!!

⭐️key point⭐️

  • {Get}プロトコルに従うと{Get}、{GetSet}は1つしか実現できません!
  • {get set}すべて実現!
  • すべての
  • 宣言の方法とプログラムを実現します!
  • でもやりたくないのは?ㅠㅠ回答)@ojbcオプションを使う~
    @objc protocol CoordinatorProtocol {
    var childCoordinator : [CoordinatorProtocol] { get }
    @objc optional func goingStart()
  • 簡単ですよね?ほほほ、精霊も覚えておけばよかった!!
    協議は重要で、勉強は必要です.私ももっと
    うん...では.
    皆さん.
    private let window : UIWindow
    init(window:UIWindow) {
    self.window = window
    }
    それを知っておくべきだと思いますが、省略します!!
    これがSceneDelegateとつながっている部分だということはご存知でしょう?!ほほほ
    もちろんです.
    func goingStart() {
    let navigationController = UINavigationController()
    let dongGangAjiCoordinator = DongGangAjiCoordi(navigationController: navigationController)
    childCoordinator.append(dongGangAjiCoordinator)
    dongGangAjiCoordinator.goingStart()
    window.rootViewController = navigationController
    window.makeKeyAndVisible()
    }
    gongStartメソッドは何ですか!1行目から見ましょう!
    まずビューを切り替えるためにSegeではなくNavigationを使います!
    ナビゲーションコントローラのインスタンスを作成します!ほほほ
    あ、でも東江AjiCoordi<いきなり出てきたのか!?この犬の糞は何ですか.ぶるぶる震える
    この和弦を見ればいい!ほほほ
    final class DongGangAjiCoordi : CoordinatorProtocol {
        private(set) var childCoordinator: [CoordinatorProtocol] = []
        
        private let navigationController : UINavigationController
        
        init(navigationController : UINavigationController ) {
            self.navigationController = navigationController
        }
        func goingStart() {
            let dongGangAjiVC = DongGangAjiViewController.instantiate()
            let ddongViewModel = DongGangAjiViewModel()
            dongGangAjiVC.ddongViewModel = ddongViewModel
            navigationController.setViewControllers([dongGangAjiVC], animated: false)
        }
    }
    見てると~もう出来てるよハハ
    まずこの部分はfinalに疑問があるかもしれないので説明しましょう!
    まずfinalは値タイプの構造について宣言しません!!
    classの接頭辞として使用できます
    finalが宣言したクラスは継承を阻止します!だからtimeenumやstructに値してfinalを得ることができなくて、ほほほ
    したがって、再定義を必要としないclassに宣言するだけでいいです.
    DongGangAjiCoordiから協調協定を引き継いだので~
    Propertyと方法を実施するので、実施します!!
    ここで、Coordinatorのリモコンをクリックすると、このセクションが起動します!
    init(navigationController : UINavigationController ) {
    self.navigationController = navigationController
    }
    func goingStart() {
    let dongGangAjiVC = DongGangAjiViewController.instantiate()
    let ddongViewModel = DongGangAjiViewModel()
    dongGangAjiVC.ddongViewModel = ddongViewModel
    navigationController.setViewControllers([dongGangAjiVC], animated: false)
    }
    こちらはちょっと複雑かも!まず、先ほどCoordinatorからinitに移行したnavigationをproperty navigationとして受け入れます!そのNavigationにViewControllerを渡すと画面にViewControllerが表示されます!ほほほ
    そして~私はViewControllerを作成します!!
    class DongGangAjiViewController: UIViewController {
       //MARK: - Properties
         
       //MARK: - storyboard.instantiateViewController
        static func instantiate() -> DongGangAjiViewController {
            let storyboard = UIStoryboard(name: "Main", bundle: .main)
            let controller = storyboard.instantiateViewController(withIdentifier: "DongGangAjiViewController") as! DongGangAjiViewController
            return controller
        }
        //MARK: -라이프사이클
        override func viewDidLoad() {
            super.viewDidLoad()
        
        }
    
    }
    今からここで放送します~storyboard@をセットしておきます

    classを作成したばかりのViewControlに変更します.
    シーケンスイメージボードIDを変更してinstantiate()コードを記述することもできます.
    そして必ずUse Storyboard IDボタンを押してくださいね
    Navigationを使っているViewControllerを知りました!