-1

I'm coding my first app in XCode 4.2 supporting iOS 4.x through the current releases. To support iOS 4.0, I'm not using Storyboard feature and also using unsafe_unretained for weak references. I have AppDelegate files (.h and .m) along with several view controllers with UITabBarController. In my first view controller, in the -viewDidLoad method, I initialize two NSDictionaries and also start a timer with 1 sec interval. In the selector method, I have to pickup a random number between 0 and 7 to pick a corresponding value in both the dictionaries. The dictionaries are used only in the first view controller and not anywhere.

My first question is

  1. where do I load those two dictionaries - in the AppDelegate -didFinishLaunchingWithOptions: method or in the first view controller's -viewDidLoad method?
  2. I also wanted to support iPad. If that's the case, do I create a common class library to support iPhone/iPod/iPad?. If that is the recommended way, can I move the common functionality to the AppDelegate .m file instead?

Please advise.

iHunter
  • 6,205
  • 3
  • 38
  • 56

2 Answers2

0

You can move your common data and business logic into a separate set of model classes outside of the UI layers and appdelegates. This is one of the main benefits of the MVC pattern - by separating and making a clear distinction, it's easy to have separate view layers (one for phone and one for iPad).

That means all the data (dictionaries), logic with your random numbers and timers would be encapsulated and shared. That also allows you to cleanly unit test the majority model and logic programmatically. It also means you can make substantial changes to your algorithms and minimize the code churn.

When the timer goes off it can either post a notification or you can have a delegate pattern where it does a call back.

Related Post: Delegates vs. events in Cocoa Touch

If you do a shared model, one option is to use a singleton pattern where you access the model like:

[MyModel sharedInstance];
Community
  • 1
  • 1
bryanmac
  • 38,941
  • 11
  • 91
  • 99
  • 1
    Excellent. Can you please show me a quick example of how to create model classes. When you mean "view layers", do you mean separate views (.xib, .h and .m) for iPhone and iPad? – Appium_ios_android Dec 30 '11 at 19:10
  • The model classes are nothing more than objective-c classes with a .h and .m. As for the singleton, search for objective-c singleton. – bryanmac Dec 30 '11 at 19:14
  • also read: http://stackoverflow.com/questions/2411507/singleton-where-to-create-instance – bryanmac Dec 30 '11 at 19:15
0

You should keep your code and data together if possible, which means that if you only access the dictionaries in your view controller then you should initialize them in the view controller's viewDidLoad.

I recommend keeping stuff out of the app delegate if possible or else you'll end up with a weird monster class that does too much stuff for which it shouldn't be responsible. If necessary, create one or more classes that manage common data (for example using the singleton pattern).

Whether you can/should use a common file for both iPhone and iPad depends on a number of factors. The main factor is: how different are the UIs? If they are very similar then use one class for both. You can also create a base class with common functionality and sub classes for iPhone and iPad which implement the necessary differences.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • Thanks for all your replies. I quickly read the link that the previous gentleman posted but it is confusing to me. A quick singleton example with a sample dictionary initialization code along with how to use it would be immensely helpful for a newbie. – Appium_ios_android Dec 30 '11 at 19:25