views:

140

answers:

2

Hello,

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;
}
+4  A: 

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?

mouviciel
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
Right. I don't know whether my answer will solve OP question, but this is still a problem.
mouviciel
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
there is no other way?
omri
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
what do you mean by email?
omri
LOL./!! I would avoid NSNotificationCenter. Just use Proxy pattern and a NSMutableArray of "subdelegates"*, plural. *coin a phrase. ;)
martinr
:-) can you give an example?
omri
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
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
+1  A: 

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
This is way too much work for a negligible benefit. NSNotificationCenter is plenty fast enough for UI implementation.
NSResponder
I told you there were two camps.
martinr
"I told you there were two camps" - lol
JamesC