[1] StoryboardSupport

//
//  StoryboardSupport.swift
//
//
//  Created by Richard Critz on 11/3/16.
//
//  Based on work by Andyy Hope (github.com/andyyhope)
//
//  Updated for Swift 4 by Audrey Tam on 11/6/17.
//

import UIKit

protocol StoryboardIdentifiable {
  static var storyboardIdentifier: String { get }
}

extension StoryboardIdentifiable where Self: UIViewController {
  static var storyboardIdentifier: String {
    return String(describing: self)
  }
}

extension StoryboardIdentifiable where Self: UICollectionViewCell {
  static var storyboardIdentifier: String {
    return String(describing: self)
  }
}

extension StoryboardIdentifiable where Self: UITableViewCell {
  static var storyboardIdentifier: String {
    return String(describing: self)
  }
}

extension UIViewController: StoryboardIdentifiable { }
extension UICollectionViewCell: StoryboardIdentifiable { }
extension UITableViewCell: StoryboardIdentifiable { }

extension UIStoryboard {

  //  If there are multiple storyboards in the project, each one must be named here:
  enum Storyboard: String {
    case Main
  }

  convenience init(storyboard: Storyboard, bundle: Bundle? = nil) {
    self.init(name: storyboard.rawValue, bundle: bundle)
  }

  class func storyboard(storyboard: Storyboard, bundle: Bundle?) -> UIStoryboard {
    return UIStoryboard(name: storyboard.rawValue, bundle: bundle)
  }

  func instantiateViewController<T: UIViewController>() -> T {
    guard let viewController = instantiateViewController(withIdentifier: T.storyboardIdentifier) as? T else {
      fatalError("Could not find view controller with name \(T.storyboardIdentifier)")
    }

    return viewController
  }

}

extension UICollectionView {
  func dequeueReusableCell<T: UICollectionViewCell>(for indexPath: IndexPath) -> T {
    guard let cell = dequeueReusableCell(withReuseIdentifier: T.storyboardIdentifier, for: indexPath) as? T else {
      fatalError("Could not find collection view cell with identifier \(T.storyboardIdentifier)")
    }
    return cell
  }

  func cellForItem<T: UICollectionViewCell>(at indexPath: IndexPath) -> T {
    guard let cell = cellForItem(at: indexPath) as? T else {
      fatalError("Could not get cell as type \(T.self)")
    }
    return cell
  }
}

extension UITableView {
  func dequeueReusableCell<T: UITableViewCell>(for indexPath: IndexPath) -> T {
    guard let cell = dequeueReusableCell(withIdentifier: T.storyboardIdentifier, for: indexPath) as? T else {
      fatalError("Could not find table view cell with identifier \(T.storyboardIdentifier)")
    }
    return cell
  }

  func cellForRow<T: UITableViewCell>(at indexPath: IndexPath) -> T {
    guard let cell = cellForRow(at: indexPath) as? T else {
      fatalError("Could not get cell as type \(T.self)")
    }
    return cell
  }
}

/// Use in view controllers:
///
/// 1) Have view controller conform to SegueHandlerType
/// 2) Add `enum SegueIdentifier: String { }` to conformance
/// 3) Manual segues are trigged by `performSegue(with:sender:)`
/// 4) `prepare(for:sender:)` does a `switch segueIdentifier(for: segue)` to select the appropriate segue case

protocol SegueHandlerType {
  associatedtype SegueIdentifier: RawRepresentable
}

extension SegueHandlerType where Self: UIViewController, SegueIdentifier.RawValue == String {

  func performSegue(with identifier: SegueIdentifier, sender: Any?) {
    performSegue(withIdentifier: identifier.rawValue, sender: sender)
  }

  func segueIdentifier(for segue: UIStoryboardSegue) -> SegueIdentifier {
    guard   let identifier = segue.identifier,
      let segueIdentifier = SegueIdentifier(rawValue: identifier)
      else {
        fatalError("Invalid segue identifier: \(String(describing: segue.identifier))")
    }

    return segueIdentifier
  }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 有个地方 那里的一草一木一山一水 一门一框一砖一瓦 不用任何铺垫每一个人都会动情 有个地方 那里有纯朴熟悉的乡音 ...
    水稻人生阅读 670评论 0 1
  • 以为人生是一杯浊酒 时光不语默默溜走 挣扎过又放弃的自由 如今流浪在哪个街头 落叶纷飞情境如旧 明月无声独照空楼 ...
    墨染雪莲阅读 291评论 1 1
  • 金九银十求职季,加入到求职大军中的我们,在真正求职之前,需要先规划好自己的职业发展。先订一个小目标,比如:三个月完...
    发现幸福阅读 216评论 0 0
  • 登峰留影——我的玉龙 雪山之行 一直以来都很喜欢看雪,喜欢白色纯净的感觉,15年年初我终于看到了梦寐以求的雪山,在...
    七月hope阅读 313评论 0 1
  • 人在天地间, 生路有长短。 命为一呼一吸, 无论贵与贱。 是非功过名利, 爱恨悲欢离合, 悠悠云和烟。 耿耿赤子心...
    曹焕甫阅读 170评论 2 3