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;
}
3
  • 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? Commented Dec 18, 2009 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? Commented Dec 18, 2009 at 14:24
  • Yes, the first object no longer receives the accelerometer events, how can i use the events for more then one object? Commented Dec 18, 2009 at 14:51

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

7 Comments

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...
Right. I don't know whether my answer will solve OP question, but this is still a problem.
I think that is the problem, how can i do it with more then one object? pic1 = [[peopleAccel alloc] init]; pic2 = [[peopleAccel alloc] init];
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.
LOL./!! I would avoid NSNotificationCenter. Just use Proxy pattern and a NSMutableArray of "subdelegates"*, plural. *coin a phrase. ;)
|
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];

2 Comments

This is way too much work for a negligible benefit. NSNotificationCenter is plenty fast enough for UI implementation.
I told you there were two camps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.