0

I have a custom UIImageView class that I want to use to control my UIImageViews.

Inside the custom init method I have the following regarding the UIAccelerometer:

    UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
    accelerometer.updateInterval = 0.5f;
    accelerometer.delegate = self;

My problem is that I have multiple instances of this class and the accelerometer only sends information to the last instance I create. I know the reason for this is because the delegate is being set to the last instance, so my question is:

Is there a way to set the delegate to all of these instances so all of them receive the "didAccelerate" call? If so then how would I go about it.

Fellowsoft
  • 340
  • 5
  • 11

1 Answers1

1

Disclaimer: the method you're trying to use is deprecated. Use CMMotionManager instead. However:

Set one delegate (it should be a separate class), then use NSNotificationCenter to distribute the information to the other listeners. Example:

@interface SharedAccelerometerListener: NSObject <UIAccelerometerDelegate>
@end

@implementation SharedAccelerometerListener

- (id)init
{
    if ((self = [super init]))
    {
        [UIAccelerometer sharedAccelerometer].delegate = self;
        [UIAccelerometer sharedAccelerometer].updateInterval = 0.05f;
    }
}

- (void)accelerometer:(UIAccelerometer *)acc didAccelerate:(UIAcceleration *)acceleration
{
    NSDictionary *info = [NSDictionary dictionaryWithObject:acceleration forKey:@"acceleration"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AccelerometerDidAccelerate" sender:nil userInfo:info];
}

@end

Then in your listener class:

id accelerationListener = [[SharedAccelerometerListener alloc] init];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didAccelerate:) name:@"AccelerometerDidAccelerate" object:nil];

- (void)didAccelerate:(NSNotification *)notif
{
    UIAcceleration *acc = [[notif userInfo] objectForKey:@"acceleration"];
    // do whatever you want with the acceleration
}
  • Thanks for the update. It seems I'm sending the message to an deallocated instance. What can I use to check which instance is deallocated? – Fellowsoft Jul 30 '12 at 12:38
  • I also get a warning for not using the accelerationListener. And I have noticed that if I disable it it doesn't crash. So am I suppose to insatiate the class without using? – Fellowsoft Jul 30 '12 at 13:59
  • No. You'd better turn off ARC. –  Jul 30 '12 at 17:44
  • If you don't instantiate accelerationListener, it remains nil. Messages can be sent to nil but **they won't do anyithing so the entire logic won't work**. Please, please, try to understand what and how this code does, it's so simple! –  Jul 30 '12 at 17:49
  • I'm sorry, I only started objective-c post ARC so I'm still very new to it. Do you mean turn arc off for the entire project or just the acceleration class. I turned it off for the class but no dice. my second question was just grasping at straws really. I only asked it because I get the warning saying I don't use it. Thank you for all your help. – Fellowsoft Jul 31 '12 at 06:16
  • @Fellowsoft just for this class. And have in your mind that compiler warnings are warmings and not errors because the compiler is not sure whether it will cause the problem. Here the compiler is fooled by the fact that you don't do anything further to the object - it distributes notifications automatically. But if you don't even create it, what do you expect, really? I wrote it in the answer because it is *needed*, not just for fun. Again, please try reading and understanding what this code does. –  Jul 31 '12 at 06:20
  • I found my error. It's working now. I returned the class after the init method out of stupidity. Thank you for your patience and all your help. Objective C is not the easiest language to learn but I'll get there, eventually. – Fellowsoft Jul 31 '12 at 06:51