3
3

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 5 years have passed since last update.

【iOS】storyboardを使ったときの一つの解 〜少しでもきれいにプログラムを書きたい悪あがき〜

Posted at

背景

よくプログラムを書くときは、MVCモデルが使われます。
一方で、Modelをどう伝搬させていくか、あるいは、Modelへのリンキングをどうするかが
いつも悩みの種になります。

特にstoryboardを使用してTabViewを使用するなんて場合は、
複数のViewControllerがあるため何かと悩みの種になります。

今回は、TabViewをstoryboard上で使用する場合、どうModelオブジェクトを取得するかに
焦点を当てて、一つの解を示します。

一つの解決策

UITabBarControllerをカスタマイズして、Modelを持たせるだけ

これが一つの解です。

解決策の理由

UIViewControllerには、プロパティとして、
tabBarController
というプロパティがあるのです。

つまり、タブ一つ一つに割り当てられるUIViewControllerではこれを使用して、

        //get parent tabbar controller
        var parent: CustomTabBarController = self.tabBarController as! CustomTabBarController
        
        //get model from parent
        var model: Model = parent.model

という風に記載すれば、Modelオブジェクトを取得出来るようになるわけです。

エンディング

いかがでしたでしょうか。
これは応用すれば、xibでも出来るかと思います。

これで、タブを使用する場合のModelの伝搬をどうするかについて悩まなくても良くなると
思います!!!

それではまた〜(^^)/

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?