我有一個Card類從UIView和從NSObject Deck類分類。 Card在繼承的UIView之上有幾個整數屬性,Deck有一個用於存放一些卡的NSMutableArray。生成一副牌後,我想顯示一張隨機選擇的牌(通過將其添加到超級視圖中)。在我做之前,我會檢查是否已經有一張卡片,我打電話給方法在請求一張新卡片之前將其釋放。但是我在標題中得到了警告。下面的代碼...'卡'可能不會響應'-fadeAway'
#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];
}
我在編程初學者(比其他基礎!),所以我仍然包裹我的頭圍繞整個對象的事。感謝您的任何幫助和/或建議!
編輯: 這裏的卡和甲板代碼...
#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
爲什麼你需要'(id)發送者只是好奇? – 2010-07-08 02:42:29
不確定。我的邏輯是我需要將aCard實例發送給該方法,以便知道要發佈的內容。 – Steve 2010-07-08 03:18:28