我有一个来自UIView的Card类和一个来自NSObject的Deck类。 Card在继承的UIView之上有几个整数属性,而Deck有一个NSMutableArray用于存放一些卡。生成一副牌后,我想显示一张随机选择的牌(通过将其添加到超级视图中)。在我这样做之前,我会检查是否已经存在卡片,在调用新卡片之前我会调用一种方法来释放它。但我在标题中收到了警告。这是代码......
#import <UIKit/UIKit.h>
#import "Card.h"
#import "Deck.h"
@interface FlashTestViewController : UIViewController {
Deck* aDeck;
Card* aCard;
}
- (IBAction)generateDeck;
- (IBAction)generateCard;
- (void)fadeAway:(id)sender;
@end
#import "FlashTestViewController.h"
@implementation FlashTestViewController
- (IBAction)generateDeck {
if (aDeck != nil) {
[aDeck release];
}
aDeck = [[Deck alloc] initDeckWithOperator:@"+"];
}
- (IBAction)generateCard {
if (aCard != nil) {
[aCard fadeAway];
}
aCard = [aDeck newCardFromDeck];
[self.view addSubview:aCard];
}
- (void)fadeAway:(id)sender {
[aCard removeFromSuperview];
[aCard release];
}
我是编程的初学者(除了Basic!)所以我仍然围着整个对象做事。感谢您提供任何帮助和/或建议!
编辑: 这是卡片和卡片代码......
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@class Card;
@interface Card : UIView {
int upperOperand;
int lowerOperand;
NSString* theOperator;
int theResult;
}
@property(nonatomic) int upperOperand;
@property(nonatomic) int lowerOperand;
@property(nonatomic, retain) NSString* theOperator;
@property(nonatomic) int theResult;
@end
#import "Card.h"
@implementation Card
@synthesize upperOperand;
@synthesize lowerOperand;
@synthesize theOperator;
@synthesize theResult;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
self.backgroundColor = [UIColor redColor];
self.layer.cornerRadius = 15;
self.alpha = 0.3;
self.layer.borderColor = [[UIColor blueColor] CGColor];
self.layer.borderWidth = 4;
}
return self;
}
- (void)dealloc {
[super dealloc];
}
@end
#import <Foundation/Foundation.h>
#import "Card.h"
@class Deck;
@interface Deck : NSObject {
NSMutableArray* cards;
}
@property(nonatomic, retain) NSMutableArray* cards;
- (id)initDeckWithOperator: (NSString*)mathOper;
- (id)newCardFromDeck;
@end
#import "Deck.h"
@implementation Deck
@synthesize cards;
- (id)initDeckWithOperator: (NSString*)mathOper {
if (cards != nil) {
[cards release];
}
cards = [[NSMutableArray alloc] init];
for (int i=0; i<11; i++) {
for (int j=0; j<11; j++) {
Card* aCard = [[Card alloc] initWithFrame:CGRectMake(10, 10, 60, 80)];
aCard.upperOperand = i;
aCard.lowerOperand = j;
aCard.theOperator = mathOper;
aCard.theResult = i + j;
[cards addObject: aCard];
[aCard release];
}
}
return self;
}
- (id)newCardFromDeck {
int index = random() % [cards count];
Card* selectedCard = [[cards objectAtIndex:index] retain];
[cards removeObjectAtIndex:index];
return selectedCard;
}
@end
答案 0 :(得分:4)
您已为fadeAway
类定义了FlashTestViewController
方法,而不是Card
类。这意味着您只能在Card
类的实例上调用此方法(或根据您首选的OOP术语发送消息)。
所以[aCard fadeAway]
是不正确的,因为它占用了错误数量的参数,但也因为aCard是Card
类实例,并且没有为该类定义fadeAway:
方法(以及,我们没有看到它的定义,所以可能是但不明显如此)。
但是你没有显示Card
类的定义,所以也许你可以在那里定义方法。
答案 1 :(得分:1)
您将fadeAway
声明为采用id
类型的单个参数,因此您必须以这种方式调用它。所以:
[aCard fadeAway:nil];
此外,由于它从不对其参数做任何事情,你可以简单地声明它不接受一个,然后按原样调用它。所以:
-(void)fadeAway;
// later
[aCard fadeAway];
答案 2 :(得分:0)
您需要在参数中说明aCard
(id)sender
{{1}}。你没有给出足够的论据来换句话说。