views:

15

answers:

2

Hello,

I would like to use the accelerometer in a tab app. All three tabs would access the current x,y,z and change an mage based on that data. My problem is where does the initialization of the accelerometer go so that all three tabs can update based on the data? The update only needs to happen when you are in the current tab.

I can only get this to 'work' if I initialize 3 accelerometers which is not correct. Would the data go in my appdelegate and the code to change the images in each nibs view controller?

A: 

Hi there, Try to avoid putting code into appdelegate. Try using appdelegate only to handle basic stuff. I would recomend you to implement a Singleton-Class for your Accelerometer Data.

Singletons are a very basic design pattern, that come in handy especiall in the environment of mobile devices (fewer ressources than a pc)

you can read about the singleton Pattern here: Wikipedia: Singleton Pattern

also there is a very easy way to synthesize a singelton class in your project

include the follwing file into your project syntesizesingleton.h

and within your accelerometer class put the following

@implementation yourAccelerometerClass
SYNTHESIZE_SINGLETON_FOR_CLASS(yourAccelerometerClass)

afterwards within your view controllers (or wherever) you can acces the singleton-accelerometer class by dong things similar to the following:

[[yourAccelerometerClass sharedyourAccelerometerClass] yourMethod];

hope that helps.
sam

samsam
A: 

Thank you so much - that worked for me.

malaki1974