2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Swift】なんで流行っていないんだ??便利ライブラリ Instantiate

Last updated at Posted at 2021-04-01

どういうことか

とても汎用性があって便利なのになぜかあまり使っている記事を見ない。

Instantiate
https://github.com/tarunon/Instantiate

なにが便利か

例えばNavigation遷移する際に書くこんなコードが

let storyboard = UIStoryboard(name: "NextViewController", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "NextViewController")

self.navigationController?.pushViewController(vc, animated: true)

こう書ける

let vc = NextViewController.instantiate()

self.navigationController?.pushViewController(vc, animated: true)

どう使うのか

まずライブラリをインストールする。
それから、ここで言う NextViewController の場合

NextViewController.swift
import UIKit
import Instantiate
import InstantiateStandard

class NextViewController: UIViewController, StoryboardInstantiatable {
    // 略
}

あとは

  • ViewControllerの名前とStoryboardの名前を同じ名前にする必要がある
  • Storyboardの Is Initial View Controller に忘れずチェックをする

【Swift】ライブラリ Instantiate を使用した際のエラー "Fatal error: Unexpectedly found nil while unwrapping an Optional value"
https://qiita.com/antk/items/24dc77347e12f2b63bac

詳しくはドキュメントを見てください(ドキュメントに載っていないこともあるんですが)。

おわり(´・ω・`)

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?