0

Is it possible to somehow store a UIView instance in such a way that it can be accessed from other view controllers? I know this is probably bordering on "globals" which they say are bad, but I digress. All I know is I have a couple UITabBar tabs that need to reference the same instance of a view that was instantiated in one tab and needs to be displayed again in another tab. What's the best approach for doing something like that?

2
  • Just a tip about question naming, try to make it short like "Showing the same UIView instance in different UIViewControllers" Commented Aug 12, 2011 at 13:30
  • I'm bad at that! Anyway, I've now updated it to your suggestion! :) (Previously "Store a UIView instance so that it can be called when needed from other view controllers after it has already been hidden by other views") Commented Aug 12, 2011 at 14:06

3 Answers 3

1

Sure. You just need to store a retained reference to the UIView object in a persistent object. For example, you can add a retained property to your UIApplicationDelegate subclass. You can have that delegate instantiate the view, and all the controllers would just ask the app delegate for the view. If you have a root view controller that is always available, you could retain it there.

Maybe thinking through the overall structure of your app can help find the "right" place to store the UIView. Below I present an app structure I frequently use, not necessarily as advice on how you should structure your app, but as an example to expand the options you can consider to help you with thinking about the best structure for you app.

I write a lot of brochure like apps for our clients. In these apps I need to present a number of views, each somewhat analogous to pages in a brochure. Some of these views will have user interaction, and need to retain their state when off screen, or share the state data with other views.

To manage these apps I create a presentation manager object. This object will retain the overall state of the app, including views that must persist when not displayed. I use one master UIViewController that owns the presentation manager and is responsible for all common view control operations. The specific logic for individual views will go in UIView subclasses for each view. These individual views are created by the presentation manager, and can ask that manager for what it knows, including any persistent views.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, sounds like my app is pretty similar to yours in what it needs to do... retain the state of a view when off screen.
1

You can just use dependency injection, to inject the same view instance to the view controllers like this:

UIView *myView = [UIView new];
UIViewController *controller1 = [UIViewController new];
UIViewController *controller2 = [UIViewController new];
controller1.view = myView;
controller2.view = myView;
[myView release];

1 Comment

Interesting. I hadn't thought of doing it like that, but I'm pretty sure this won't work with UITabBar views because the UITabBarController will instantiate each tab's view itself independently of the instance I would instantiate using the code above. Any ideas how to work around that issue?
0

B/c you use UITabBar I would suggest to add your custom view to the window in the app delegate. Then you don't have to store it, just hide it. You can use either NSNotificationCenter to send notifications to show the view or you can call your appDelegate and show the view manually.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.