2010-07-08 21 views
0

我有一個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 
+0

爲什麼你需要'(id)發送者只是好奇? – 2010-07-08 02:42:29

+0

不確定。我的邏輯是我需要將aCard實例發送給該方法,以便知道要發佈的內容。 – Steve 2010-07-08 03:18:28

回答

4

您已經定義爲FlashTestViewController類,而不是CardfadeAway方法。這意味着您只能在Card類的實例上調用此方法(或根據您的首選OOP術語發送消息)。

所以[aCard fadeAway]不正確,因爲它需要錯號碼則params的,也因爲ACARD是Card類實例和fadeAway:方法沒有爲該類定義(當然,我們並沒有看到它的定義,所以也許它是但不是如此)。

但是您沒有顯示Card類的定義,因此您可能會在那裏定義方法。

+0

我在Card類中沒有fadeAway方法。我仍然對功能的不同位置應該去哪裏感到困惑。我認爲解卡的方法應該放在控制器中,但也許應該放在卡片類中。我將發佈我的卡片和卡片代碼作爲答案。 – Steve 2010-07-08 03:07:23

+0

其實 - 我會編輯原來的問題,因爲友好的網站向我暗示...... =) – Steve 2010-07-08 03:08:26

+0

我把fadeAway方法放入Card類中,沒有發件人和 - 瞧!有用!當我構建一副撲克牌時,我用隨機數字來製作幀的x和y位置,並且每次點擊New Card按鈕時,舊的牌會消失,而新的牌會出現在別的地方。現在讓它淡入淡出,並將內容放入UIView。 非常感謝您的幫助!我會將答案投票,但我還沒有足夠的聲望。 – Steve 2010-07-08 04:52:28

0

您需要爲aCard提供一個(id)sender,如您在參數中所述。你沒有給出足夠的論據來換句話說。

1

您將fadeAway聲明爲採用id類型的單個參數,因此您必須這樣調用它。所以:

[aCard fadeAway:nil]; 

而且,因爲它從來不與它的參數任何東西,你可以簡單地聲明它不會採取一個,然後把它原樣。所以:

-(void)fadeAway; 

// later 
[aCard fadeAway]; 
相關問題