1

Using the "Tab Bar" template in Xcode 4, each ViewController is created automatically, so I don't have the chance to set any properties on a ViewController as it's created. I want each ViewController to have access to an object (an instance of FMDB's FMDatabase).

There are a number of questions on StackOverflow relating to this already, but some are assuming that you are creating the VC's by hand, and others recommend using the AppDelegate as a mediator:

MyAppDelegateClass *appDelegate = [[UIApplication sharedApplicaton] delegate];
myLocalProperty = appDelegate.someDataModelProperty;

The above seems rather "hacky" to me. Is there a better way to access a single object from multiple ViewControllers?

Drarok
  • 3,612
  • 2
  • 31
  • 48
  • Even with a Tab Bar template, you will have custom subclasses of view controllers. Most probably they will `FirstViewController` and `SecondViewController`. Check it out. You can make your changes there. – Deepak Danduprolu Jun 03 '11 at 13:37
  • I wanted an answer to this question as well. Nice timing ! – Legolas Jun 03 '11 at 13:39
  • I dont think it is "hacky", if it doesn't need to change based on views, it is easier than passing it to each subview's controller. it is good practice to make it readonly, so that subsequent objects can modify it, but not surreptitiously replace it, if that works for the data you are handling. – Grady Player Jun 03 '11 at 22:30
  • @Grady Player: it feels a little hacky to me, and I found an article comparing the clutter people place in the AppDelegate to the olden-days practice of global variables for everything. I think that's what felt wrong about it to me. – Drarok Jun 03 '11 at 23:40
  • @Deepak: I couldn't see a way to pass an object to the VCs' init methods. Such as [[MySecondVC alloc] initWithDatabase:_db]; It's a moot point now anyway. :) – Drarok Jun 03 '11 at 23:42

3 Answers3

3

Most probable implementation of FMDB's Data base would be to implement it as a Singleton and access it through out the application. Is there any reason for not doing so ?

This link should help you in doing so: How do I make FMDB's database a singleton

Community
  • 1
  • 1
Tatvamasi
  • 2,537
  • 19
  • 14
  • BTW--the example you link to could suffer from a race condition of 2 threads call `+sharedSQLiteDB` "simultaneously". You could create your singleton instance in `+(void)load` instead or add a lock around the code in `+sharedSQLiteDB` – nielsbot Jun 03 '11 at 22:27
  • I didn't fancy sprinkling SQL queries throughout my Controllers, but based on this I've created a singleton class that manages my database interaction in a much more Cocoa-ish way, thanks! – Drarok Jun 03 '11 at 23:43
0

To expand on KKK4SO's answer, you could implement the Database as a singleton. Then, have all of your UIViewControllers be a subclassed UIViewController that contains methods to access and modify the Database singleton.

Sid
  • 9,508
  • 5
  • 39
  • 60
0

MyAppDelegateClass *appDelegate = [[UIApplication sharedApplicaton] delegate]; myLocalProperty = appDelegate.someDataModelProperty;

There is a spelling error in this code 'sharedApplicaton' is incorrectly spelled it should be 'sharedApplication'.

Shame on me for copy and pasting! Anyway this will work for a quick and dirty way of passing data between ViewControllers etc.