0

This might sound like duplicate, but I need farther understanding and explanation about my Scenario please.

In my app, I have one InfoViewController, which represents information when called (pushed)from other Controller like, 1)HomeViewController, 2)FavouriteViewController and 3)DownloadViewController. All 3 held by UITabViewController

InfoViewController had about 10 buttons and corresponding actions. I use separate singleton to hold all info objects.

All 3 ViewController(Held by TabVC)--> Loads object to Singleton --> InfoViewController uses that to Present Detail Info

HomeViewController - Its for new 'Info' to present,which changes everyday When Information is present,

user can mark it as a Favorite, which then Listed(In UITableView) on FavoriteViewController. User can save it in phone for future reference, which will be listed(In UITableView) on DownloadViewController.

On selection of cell all 3 represents details using InfoViewController.

Now I want to present only one instance on InfoViewController to be visible to user. Not all in all 3 tab. Currently I am switching it back to Main screen with ViewDidDisappear method. Which works only when I add InfoViewController as child to main 3 VC. Not by push.

Now my problem is, i tried to use Appdelegate to initialized SharedInfo Object

sharedInfo = [InfoViewController alloc]init], but it goes to black screen. I have to initialize it as

[self.storyboard instantiateViewControllerWithIdentifier:@"InfoViewController"];

but this is not allowed in AppDelegate or making shared instance on InfoViewController itself.

How do I achieve Only one Instance present to user at a time??

1
  • Have you considered having only one that you pass as a reference to whatever needs it.. Or, create a controller to whom you send messages from wherever you need it? Honestly, though, this does violate how these things are intended to be built. Commented Dec 10, 2015 at 4:25

1 Answer 1

1

I think rephrasing what you need might help. I don’t think you really want a shared instance of the UIViewController. What you want is a single source of the data which is displayed in one or more views and a method to update that data in all the views when the data changes.

Two possible solutions are:

NSNotification Any view which contains the shared data subscribes to notifications when the data is changed. When data is updated via the singleton it sends a broadcast to anyone listening to update the view data.

View Lifecycle In this second method the data in a particular view is only updated when the user brings up that view. Note in a tab bar interface that the viewDidLoad is called when the view is initially loaded. DO NOT update your data here if it changes. Instead you want to update the data in viewWillAppear as this is called every time you navigate to that view. In viewWillAppear you have code that checks the data and updates it if needed.

There are other ways to skin this cat, but either of the two above should work for you.

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

1 Comment

oh ok i understand this. i was thinking to using viewDidDisappear method to remove childVC from each tab. Thanks with your answer, I got conformation that thats the only way to do it. Some time its confusing to decide which way to go. Thanks again xdeleon

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.