★ 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
参考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 {}