views:

425

answers:

3

hi, i tried to implement this code that move UIImageView's objects,
i get a compiler warning for the objects (jumpBall1...) in the createPosition method: UIImageView may not respont to -setupperLimit, -setlowerLimit, -setspeed

code:

@interface JumpBallClass : UIViewController
{
    CGPoint center;
    CGPoint speed;

    CGPoint lowerLimit;
    CGPoint upperLimit;

    IBOutlet UIImageView *jumpBall1;
    IBOutlet UIImageView *jumpBall2;
}

@property (assign) CGPoint lowerLimit;
@property (assign) CGPoint upperLimit;
@property (assign) CGPoint speed;
@property(nonatomic,retain) IBOutlet UIImageView *jumpBall1;
@property(nonatomic,retain) IBOutlet UIImageView *jumpBall2;


- (void)update;

@end

@implementation JumpBallClass
- (void)update
{
    center.x += speed.x;
    center.y += speed.y;

    if (center.x > upperLimit.x || center.x < lowerLimit.x)
    { speed.x = -speed.x; }
    if (center.y > upperLimit.y || center.y < lowerLimit.y)
    { speed.y = -speed.y; }
}
@end

- (void) jumpOnTimer {          

 NSArray * balls = [NSArray arrayWithObjects:jumpBall1, jumpBall2, nil];

 [balls makeObjectsPerformSelector:@selector(update)];
}

- (void) createPosition { 
  [jumpBall1 setUpperLimit:CGPointMake(60, 211)];
  [jumpBall1 setLowerLimit:CGPointMake(0, 82)];
  [jumpBall1 setspeed:CGPointMake(2.0,7.0)];
  ...
}
A: 

You need to mark your properties with @synthetize in order for the compiler to generate the accessor methods. As the directive is missing, you get the warnings.

If you don't want to generate the accessors, then use the "dot" notation to access the properties.

Laurent Etiemble
While this is true, it's not the reason for the problem the OP is having - regardless of whether or not he includes a `@synthesize` for the properties on JumpBallClass, UIImageView still won't have those properties and so he still won't be able to call their setters.
Tim
You are right. I focused on the class and not on the definition and not on the members.
Laurent Etiemble
+1  A: 

You're receiving that warning because you've declared the upperLimit and lowerLimit properties on JumpBallClass, not on a subclass of UIImageView, and you're trying to call the property setters on instances of UIImageView. UIImageView has no properties with those names, so you receive the compiler warning.

Furthermore, you're missing the superclass in the interface; what does JumpBallClass extend? NSObject? UIImageView? Provide some more information and I can provide a more complete answer.

Tim
JumpBallClass is a UIViewController
dani_au
+3  A: 

Your code is attempting to call the methods setUpperLimit: setLowerLimit: and setspeed: on the member variable jumpBall1 which you have declared as a UIImageView. However these are not methods of UIImageView - they are setter methods on the @properties you have declared in your own custom JumpBallClass. You have not specified the synthesis of these properties in your implementation file however (using the @synthesize keyword).

Perhaps what you mean to do is make the JumpBallClass a subclass of UIImageView and instantiate two instances of this, one for each jumpBall which you would like to control.

@interface JumpBallClass : UIImageView
{ 
CGPoint center; 
CGPoint speed; 

CGPoint lowerLimit; 
CGPoint upperLimit; 

IBOutlet UIImageView *jumpBallView; 
} 

@property (assign) CGPoint lowerLimit; 
@property (assign) CGPoint upperLimit; 
@property (assign) CGPoint speed; 
@property(nonatomic,retain) UIImageView *jumpBallView; 

- (void)update; 

@end 

@implementation JumpBallClass
@synthesize lowerLimit, upperLimit, speed, jumpBallView;

- (void)update 
{ 
    center.x += speed.x; 
    center.y += speed.y; 

    if (center.x > upperLimit.x || center.x < lowerLimit.x) 
    { speed.x = -speed.x; } 
    if (center.y > upperLimit.y || center.y < lowerLimit.y) 
    { speed.y = -speed.y; } 
} 
@end 

//assuming jumpBall1&2 have beeen declared like so:
//JumpBallClass *jumpBall1=[[JumpBallClass init] alloc];
//JumpBallClass *jumpBall2=[[JumpBallClass init] alloc];

- (void) jumpOnTimer {               

NSArray * balls = [NSArray arrayWithObjects:jumpBall1, jumpBall2, nil];     

[balls makeObjectsPerformSelector:@selector(update)];     
}     

- (void) createPosition {      
[jumpBall1 setUpperLimit:CGPointMake(60, 211)];     
[jumpBall1 setLowerLimit:CGPointMake(0, 82)];     
[jumpBall1 setspeed:CGPointMake(2.0,7.0)];     
...     
}     
mikecsh
JumpBallClass is a UIViewController
dani_au
That doesn't change the fact that you are calling a method that you have defined for JumpBallClass on a UIImageView. You need to call the method on an instance of JumpBallClass.
Brad Larson
so i need to define it in a different class? where to define the jumpBall objects?
dani_au