//
// 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
}
}
[1] StoryboardSupport
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 金九银十求职季,加入到求职大军中的我们,在真正求职之前,需要先规划好自己的职业发展。先订一个小目标,比如:三个月完...