UIStoryboard 的安全使用

  1. 问题一:没有统一的、中心全局化的 UIStoryboard 字符串标识符
let name = "news"
let storyboard = UIStoryboard(name: name, bundle: nil)
let identifier = "ArticleViewController"
let viewController = storyboard.instantiateViewControllerWithIdentifier(identifier) as! ArticleViewController
  • 使用枚举
extension UIStoryboard {
    enum Storyboard: String {
        case main
        case news
        case gallery
    }
    convenience init(storyboard: Storyboard, bundle: NSBundle? = nil) {
        self.init(name: storyboard.rawValue, bundle: bundle)
    }
    class func storyboard(storyboard: Storyboard, bundle: NSBundle? = nil) -> UIStoryboard {
        return UIStoryboard(name: storyboard.rawValue, bundle: bundle)
    }
}
// let storyboard = UIStoryboard(storyboard: .main)
// let storyboard = UIStoryboard.storyboard(.main)
  1. 问题二:需要处理标示符和进行类型转换
let storyboard = UIStoryboard.storyboard(.news)
let identifier = "ArticleViewController"
let viewController = storyboard.instantiateViewControllerWithIdentifier(identifier) as! ArticleViewController
  • 使用协议,协议扩展,扩展,泛型
// 协议
protocol StoryboardIdentifiable {
    static var storyboardIdentifier: String { get }
}
// 协议扩展
extension StoryboardIdentifiable where Self: UIViewController {
    static var storyboardIdentifier: String {
        return String(self)
    }
}
// 全局一致性
extension UIViewController : StoryboardIdentifiable { }
// 泛型
extension UIStoryboard {
    func instantiateViewController() -> T {
        let optionalViewController = self.instantiateViewControllerWithIdentifier(T.storyboardIdentifier)
        
        guard let viewController = optionalViewController as? T  else {
            fatalError("Couldn’t instantiate view controller with identifier \(T.storyboardIdentifier) ")
        }
        
        return viewController
    }
}
  1. 安全使用
class ArticleViewController : UIViewController{
    func printHeadline() { }
}
let storyboard = UIStoryboard.storyboard(.news)
let viewController: ArticleViewController = storyboard.instantiateViewController()
viewController.printHeadline()
presentViewController(viewController, animated: true, completion: nil)

你可能感兴趣的:(UIStoryboard 的安全使用)