0

I have an object that i intalize it's propertis with call to an init function, it works fine, when i tried to add another object and intalize it the first object didn't get the properites, how do i initalize it to more then one object with diffrent or the same properties?

- (void)viewDidLoad {   
    pic1 = [[peopleAccel alloc] init];
}

Class peopleAccel:

- (id) init
{
    self = [super init];
    if (self != nil) {
        position = CGPointMake(100.0, 100.0);
        velocity = CGPointMake(4.0, 4.0);
        radius = 40.0;
        bounce = -0.1f;
        gravity = 0.5f;
        dragging = NO;
        [[UIAccelerometer sharedAccelerometer] setDelegate:self];
        acceleratedGravity = CGPointMake(0.0, gravity);
    }
    return self;
}
teabot
  • 15,358
  • 11
  • 64
  • 79
omri
  • 305
  • 1
  • 3
  • 10
  • Don't know what you mean... are you (a) trying to create an array of objects (b) trying to reinitialize your object every time the view loads / reloads (c) something else? – martinr Dec 18 '09 at 14:22
  • I see you are using the shared accelerometer? Are you perhaps finding that the first object no longer receives the accelerometer events after the second instance is created? – teabot Dec 18 '09 at 14:24
  • Yes, the first object no longer receives the accelerometer events, how can i use the events for more then one object? – omri Dec 18 '09 at 14:51

2 Answers2

4

I see a problem with setting the delegate of sharedAccelerometer. Only one object can be a delegate of another object at a time. So you should create only one peopleAccel object.

EDIT:

If you need to send accelerometer events to more than one object, you can create a specific delegate object in charge of receiving accelerometer events and broadcasting them to your several peopleAccel objects via notifications. See this question for some hints: NSNotificationCenter vs delegation?

Community
  • 1
  • 1
mouviciel
  • 66,855
  • 13
  • 106
  • 140
  • This answer is a bit 'guess' given that the person asking the problem hasn't actually stated that this is the problem that they are experiencing. Really we should encourage the asker to refine/revise the question... – teabot Dec 18 '09 at 14:27
  • Right. I don't know whether my answer will solve OP question, but this is still a problem. – mouviciel Dec 18 '09 at 14:31
  • I think that is the problem, how can i do it with more then one object? pic1 = [[peopleAccel alloc] init]; pic2 = [[peopleAccel alloc] init]; – omri Dec 18 '09 at 14:54
  • 2
    A delegate is a single pointer within the delegating object, so no, it can't point to multiple objects. Communication between that single delegate object and other objects doesn't have to be done via NSNotificationCenter--you could use email if speed isn't an issue--but you'll have to have some sort of a middle "broadcaster" object. – Dewayne Christensen Dec 18 '09 at 15:53
  • LOL./!! I would avoid NSNotificationCenter. Just use Proxy pattern and a NSMutableArray of "subdelegates"*, plural. *coin a phrase. ;) – martinr Dec 18 '09 at 16:36
  • Actually, I think that having one central accelerometer delegate which broadcasts messages via NSNotificationCenter to multiple objects listening for any accelerometer updates is an elegant solution. It decouples the accelerometer delegate and the things that care when the accelerometer reports a change. – Brad Larson Dec 18 '09 at 23:14
  • i tried using the proxy one but didn't work for me, can you post an example for using an NSNotificationCenter regarding the code above? – omri Dec 19 '09 at 14:44
1

Create a proxy so multiple objects can receive accelerometer events.

Whether you should do this or use NSNotificationCenter is debatable and there are two camps, but personally I would use this approach. NSNotificationCenter has to check string names to recognise event type; this kind of approach could be ever so slightly faster especially with a bit more optimisation. A bit more typing but I would say also easier for someone else to follow.

Something like this...

/* PLUMBING */
/* in headers */

@protocol MyAccelSubDelegate
-(void)accelerometer:(UIAccelerometer*)accelerometer
  didAccelerate:(UIAcceleration*)acceleration;
@end

@interface MyAccelSubDelegateProxy : NSObject <UIAccelerometerDelegate> {
  NSMutableArray subDelegates;
}
-(id)init;
-dealloc;
-(void)addSubDelegate:(id<MyAccelSubDelegate>)subDelegate;
-(void)removeSubDelegate:(id<MyAccelSubDelegate>)subDelegate;
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:
  (UIAcceleration *)acceleration;
@end

/* in .m file */

@implementation MyAccelSubDelegateProxy 
-(id)init { self = [super init];
  if (self!=nil) subDelegates = [[NSMutableArray alloc] init]; return self; }
-dealloc { [subDelegates release]; }
-(void)addSubDelegate:(id<MyAccelSubDelegate>)subDelegate {
  [subDelegates insertObject:subDelegate atIndex:subDelegates.count]; }
-(void)removeSubDelegate:(id<MyAccelSubDelegate>)subDelegate {
  for (int c=0; c < subDelegates.count; c++) { 
    id<MyAccelSubDelegate> item = [subDelegates objectAtIndex:c];
    if (item==subDelegate) { [subDelegates removeObjectAtIndex:c];
      c--; continue; }
  }
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:
  (UIAcceleration *)acceleration {
  for (int c=0; c < subDelegates.count; c++)
    [((id<MyAccelSubDelegate>)[subDelegates objectAtIndex:c])
      accelerometer:accelerometer didAccelerate:acceleration];
}
@end

/* SOMEWHERE IN MAIN APPLICATION FLOW STARTUP */
accelProxy = [[MyAccelSubDelegateProxy alloc] init];
[UIAccelerometer sharedAcclerometer].delegate = accelProxy;
[UIAccelerometer sharedAcclerometer].updateInterval = 0.100; // for example

/* TO ADD A SUBDELEGATE */
[accelProxy addSubDelegate:obj];

/* TO REMOVE A SUBDELEGATE */
[accelProxy removeSubDelegate:obj];

/* SOMEWHERE IN MAIN APPLICATION SHUTDOWN */
[UIAccelerometer sharedAcclerometer].delegate = nil;
[accelProxy release];
martinr
  • 3,794
  • 2
  • 17
  • 15