//
// 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
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門募闲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來步脓,“玉大人,你說我怎么就攤上這事浩螺⊙セ迹” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵要出,是天一觀的道長鸳君。 經常有香客問我,道長患蹂,這世上最難降的妖魔是什么或颊? 我笑而不...
- 正文 為了忘掉前任砸紊,我火速辦了婚禮,結果婚禮上囱挑,老公的妹妹穿的比我還像新娘醉顽。我一直安慰自己,他們只是感情好平挑,可當我...
- 文/花漫 我一把揭開白布游添。 她就那樣靜靜地躺著,像睡著了一般通熄。 火紅的嫁衣襯著肌膚如雪唆涝。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼隐解,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了诫睬?” 一聲冷哼從身側響起煞茫,我...
- 正文 年R本政府宣布痹雅,位于F島的核電站仰担,受9級特大地震影響,放射性物質發(fā)生泄漏绩社。R本人自食惡果不足惜惰匙,卻給世界環(huán)境...
- 文/蒙蒙 一技掏、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧项鬼,春花似錦哑梳、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至龄毡,卻和暖如春吠卷,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背沦零。 一陣腳步聲響...
推薦閱讀更多精彩內容
- 有個地方 那里的一草一木一山一水 一門一框一磚一瓦 不用任何鋪墊每一個人都會動情 有個地方 那里有純樸熟悉的鄉(xiāng)音 ...
- 金九銀十求職季峰弹,加入到求職大軍中的我們店量,在真正求職之前鞠呈,需要先規(guī)劃好自己的職業(yè)發(fā)展融师。先訂一個小目標粟按,比如:三個月完...