Pass a variable between lots of UIVIewControllers

后端 未结 6 612
迷失自我
迷失自我 2021-01-02 15:37

In my iPhone app, there is a setup assistant which helps users to input a lot of data. It\'s basically a UINavigationController with lots of UIViewControllers in it. Now, at

6条回答
  •  臣服心动
    2021-01-02 16:31

    I wouldn't be too quick to circumvent the data encapsulation that is such a good feature of Objective-C.

    If you are collecting something you would consider to be "Settings" consider using NSUserDefaults.

    If your views proceed in a structured one to the next to the next way, consider making a "data" class, whatever it is you're making, then pass it along from parent view to subview until you get there. (Remember that "passing" is not an expensive operation, the stuff stays put, you're passing a little pointer)

    If you really want the singleton route, consider making it a property of the application delegate (an already existing singleton)

    Remember that only the route of "passing in" the data gives the added advantage that later, maybe you will want to collect that starting information multiple times, and launch different possible last-views, and it's trivial to just pass in a different one. If you go the global route you'll then have to start re-writing everywhere you accessed it before.

提交回复
热议问题