LoginSignup
2
1

More than 3 years have passed since last update.

[Swift]R.swiftで生成するViewControllerのinitにパラメータを渡す

Last updated at Posted at 2020-05-30

背景

例によって、個人開発したScoreBoxという麻雀成績管理アプリからのネタ。

iOS13ではStoryboardでもDIができる件についてを拝見し、せっかくなのでR.swiftを利用して実装しました。
リンク先でもメリットを記載されていますが、プロパティのlet化、この恩恵は非常に大きいです。

やったこと

  • R.swiftの導入
  • Storyboard上でViewControllerにStoryboard IDを登録
    IDを指定.png

  • 呼び出し元でViewControllerのインスタンス生成/表示

let gameDetailVC = R.storyboard.main().instantiateViewController(identifier: R.storyboard.main.gameDetail.identifier) { coder in
   // GameDetailViewControllerはプロパティに`let game`を持ち、initの引数に必須としている
    GameDetailViewController(coder: coder, game: game)
}
navigationController?.pushViewController(gameDetailVC, animated: true)
2
1
3

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
1