Project/Swift+WebView

iOS/WebView/Project - BaseViewController 모듈화#8

건담아빠 2023. 1. 11. 11:25

IOS/Xcode 14X Swift5.7.2 WKWebView 에서 작업 진행

 

 

모든 ViewController들이 상속 받을수 있는 BaseViewController를 만들어서 사용하자.

  • 코드의 중복을 줄이기 위해서
  • 코드의 통일성
  • 유지보수 편의성

 

BaseViewController

import Foundation
import UIKit

class BaseViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        print("######### [BASE] ViewController - viewDidLoad")        
    }
}

extension BaseViewController {
    func showAlert(
        title: String?,
        message: String?,
        confirmHandler: (() -> Void)? = nil,
        completion: (() -> Void)? = nil)
    {
        let alertView = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let confirmAction = UIAlertAction(title: "Confirm", style: .default) { _ in
            if let handler = confirmHandler {
                handler()
            }
        }
        alertView.addAction(confirmAction)
        present(alertView, animated: true, completion: completion)
    }
    ...
}

 

InitViewController

import Foundation
import UIKit

class InitViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        ...
        title = "InitViewController"
        
        print("######### Init ViewController - viewDidLoad")
    }
}

 

MainViewController

import Foundation
import UIKit

class MainViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        print("######### Main ViewController - viewDidLoad")
    }
}

 

 

 

 

참조

https://jintaewoo.tistory.com/56

 

[Swift] 보기 좋게? UIViewController 만들어 보기

읽기 쉬운 코드 만들기를 목적으로 공부한 내용 중 일부를 정리해 보려고 합니다. 코딩을 하다보면 복잡하고 유지보수 하기 힘들도록 레거시를 만들어 내는 경우가 많습니다... 뭔가 중구난방...

jintaewoo.tistory.com

https://didu-story.tistory.com/317

 

[UIKit] BaseViewController를 사용해보자. (코드의 가독성 높이기, 중복 줄이기)

새로운 사이드 프로젝트를 하고 있는데, 거기서 사용해볼 BaseViewController에 대해서 글을 작성해겠다. 이번 프로젝트에서는 내가 해보지 않았던 것들을 조금씩 도전해보면서 만들어볼 예정이다...

didu-story.tistory.com