+1

ResponseUIViewController in IOS!

Bài hôm nay mình sẽ giới thiệu cho các bạn cách để Load UIViewController 1 cách dễ dàng hơn.

đầu tiên các bạn tạo cho 1 protocol như này.

protocol ResponseUIViewController {}
extension ResponseUIViewController where Self: UIViewController {
}

xong rồi cho UIViewController adopt protocol ResponseUIViewController :

// MARK: - Load UIViewController
extension UIViewController: ResponseUIViewController {}

Load UIViewController FromNib bình thường:

let myViewController = MyViewController(nibName: "MyViewController", bundle: nil)

cách khác:

extension ResponseUIViewController where Self: UIViewController {
    static func fromNib() -> Self {
        return self.init(nibName: String(describing: self), bundle: nil)
    }
}

và bây giờ chỉ cần.

let myViewController = MyViewController.fromNib()

Load UIViewController From Storyboard

bình thường:

let storyboard = UIStoryboard(name: "storyboardName", bundle: nil)
if let myViewController = storyboard.instantiateViewController(withIdentifier: "identifier") as? MyViewController {
    /// to do
} else {
    /// todo
}

cách khác:

extension ResponseUIViewController where Self: UIViewController {
    
    static func fromStoryboard(_ storyboardName: String, withIdentifier: String? = nil) -> Self {
        return Self.fromStoryboard(self, storyboardName: storyboardName, withIdentifier: withIdentifier)
    }
    
    fileprivate static func fromStoryboard<T: UIViewController>(_ type: T.Type, storyboardName: String, withIdentifier: String?) -> T {
        let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
        let identifier = withIdentifier == nil ? String(describing: type(of: self)) : withIdentifier!
        return storyboard.instantiateViewController(withIdentifier: identifier) as! T
    }
}


và bây giờ chỉ cần.

  1. cách này sử dụng khi Id = tên class(nên đặt Id = tên class).
let myViewController = MyViewController.fromStoryboard("Main")
  1. cách này sử dụng khi bạn muốn cài đặt Id theo cách khác.
let myViewController = MyViewController.fromStoryboard("Main", withIdentifier: "id của viewcontroller")

cách này mình đã unwrap myViewController có kiểu là MyViewController để có thể truy cập luôn thuộc tính, ví dụ:

myViewController.isLogin = true

nhược điểm của cách này là mình sẽ unwrap optional, nên nếu bạn để sai id, sai storyboard name -> crash, việc unwrap optional trong phát triển ứng dụng là bị cấm, nhưng cái gì sinh ra cũng có lý do của nó vì vậy mình nên sử dụng hợp lý, trong phần core của app các bạn phải config cẩn thận để dễ dàng phát triển, còn khi sử dụng thì nên hạn chế unwrap optional. Kết Bài

  • Ý tưởng của mình là lấy được UIViewController và ép thành kiểu mình muốn luôn để tiện sử dụng , đỡ phải check và viết code dài dòng không cần thiết, mỗi người một cách nghĩ nếu các bạn chưa thích cách này hãy custom lại theo ý mình muốn, cám ơn đã đọc bài viết của mình.

All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí