Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active February 26, 2017 00:07
Show Gist options
  • Save KentarouKanno/3d5b85a35163bfddd579 to your computer and use it in GitHub Desktop.
Save KentarouKanno/3d5b85a35163bfddd579 to your computer and use it in GitHub Desktop.
UIStoryboard

UIStoryboard

★ Storyboard取得(Main)

let storyboard = UIStoryboard(name: "Main", bundle: nil)

★ Storyboard IDを指定してViewControllerを取得

let viewController = storyboard.instantiateViewControllerWithIdentifier("StoryboardID")


// guardを使用
guard let nav = segue.destinationViewController as? UINavigationController else {
    return
}

★ StoryboardのイニシャルViewを取得

let initialviewController = storyboard.instantiateInitialViewController()

★ Modalで表示

presentViewController(viewController, animated: true) { () -> Void in
    // 表示後処理
}

★ Pushで表示

navigationController?.pushViewController(viewController, animated: true)

★ StoryboardとInitialViewを指定して遷移

// UIViewController、UINavigationControllerでも同じ呼び方でOK

let storyboard = UIStoryboard(name: "Detail", bundle: NSBundle.mainBundle())
let viewcontroller = storyboard.instantiateInitialViewController()
presentViewController(viewcontroller!, animated: true, completion: nil)

★ StoryboardとStoryboardIDを指定して遷移

// UIViewController、UINavigationControllerでも同じ呼び方でOK ※StoryboardIDを合わせる

let storyboard = UIStoryboard(name: "Detail", bundle: NSBundle.mainBundle())
let viewcontroller = storyboard.instantiateViewControllerWithIdentifier("viewcontroller")
presentViewController(viewcontroller, animated: true, completion: nil)

★ 次のViewControllerに値を設定する

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
    // NavigationController Case
    guard let navi = segue.destinationViewController as? UINavigationController else {
        return
    }
    
    // Navigation topView
    guard let next = navi.topViewController as? NextViewController else {
        return
    }
    
    // NextViewControllerのプロパティに値をセット等
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
    if let nextVC = segue.destinationViewController as? ViewController {
    
    }
}

プロジェクト

★ StoryboardのViewControllerからViewを分離するプロジェクト
VieweController

UIStoryboard Protocol

参考URL: Swift: UIStoryboard Protocol

import UIKit

class ViewController: UIViewController {
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        let storyboard = UIStoryboard(.news)
        let viewController: ArticleViewController = storyboard.instantiateViewController()
        present(viewController, animated: true, completion: nil)
    }
}

// UIStoryboardの取得に文字列を使用しないようにする
extension UIStoryboard {
    
    enum Stroyboad: String {
        case main
        case news
        case gallery
        var filename: String {
            return rawValue.capitalized
        }
    }
    
    convenience init(_ storyboard: Stroyboad, bundle: Bundle? = nil) {
        self.init(name: storyboard.filename, bundle: bundle)
    }
    
    func instantiateViewController<T: UIViewController>() -> T where T: StoryboardIdentifiable {
        guard let viewController = self.instantiateViewController(withIdentifier: T.storyboardIdentifier) as? T else {
            fatalError("Couldn't instantiate view controller with identifier \(T.storyboardIdentifier)")
        }
        return viewController
    }
}


// StoryboardIDからViewControllerの取得に文字列を使用しない

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

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

extension UIViewController: StoryboardIdentifiable {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment