2010-01-04 41 views
2

我試圖執行這段代碼中移動的UIImageView的對象,
我得到在createPosition方法的對象(jumpBall1 ...)編譯器警告:UIImageView may not respont to -setupperLimit, -setlowerLimit, -setspeediPhone代碼 - CGPoint分配問題

代碼:

@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)]; 
    ... 
} 

回答

5

你的代碼試圖調用setUpperLimit:setLowerLimit:和setspeed:在你聲明爲UIImageView的成員變量jumpBall1上。但是,這些不是UIImageView的方法 - 它們是您在自定義JumpBallClass中聲明的@properties上的setter方法。但是,您尚未在實現文件中指定這些屬性的綜合(使用@synthesize關鍵字)。

也許你的意思是讓JumpBallClass成爲UIImageView的一個子類並實例化它的兩個實例,一個用於你想要控制的每個jumpBall。

@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)];  
...  
}  
+0

JumpBallClass是一個UIViewController – 2010-01-04 12:48:49

+0

這並不改變你調用的方法,你已經爲UIImageView上的JumpBallClass定義了一個方法。您需要在JumpBallClass的實例上調用該方法。 – 2010-01-04 13:19:06

+0

所以我需要在不同的類中定義它?在哪裏定義jumpBall對象? – 2010-01-05 13:04:55

0

您需要使用@synthetize來標記屬性,以便編譯器生成訪問器方法。由於指令丟失,您會收到警告。

如果您不想生成訪問器,則使用「點」符號訪問屬性。

+0

雖然這是真實的,它不是爲OP是有問題的理由 - 不管他是否包括對JumpBallClass性質的'@ synthesize',仍然UIImageView的不會有這些屬性和所以他仍然無法打電話給他們的製片人。 – Tim 2010-01-04 12:24:03

+0

你說得對。我專注於班級,而不是定義而不是成員。 – 2010-01-04 13:17:01

1

您接收的警告,因爲你已經宣佈JumpBallClass的upperLimitlowerLimit性質,上的UIImageView的子類不,和你想呼籲的UIImageView實例的屬性設置器。 UIImageView沒有這些名稱的屬性,所以您會收到編譯器警告。

此外,您在接口中缺少超類; JumpBallClass擴展了什麼? NSObject的?的UIImageView?提供更多信息,我可以提供更完整的答案。

+0

JumpBallClass是一個UIViewController – 2010-01-04 12:33:17