开发者

'Card' may not respond to '-fadeAway'

开发者 https://www.devze.com 2023-01-06 20:29 出处:网络
I have a Card class subclassed from UIView and a Deck class from NSObject.Card has a few integer properties on top of the inherited UIView ones and Deck has an NSMutableArray for holding some cards.Af

I have a Card class subclassed from UIView and a Deck class from NSObject. Card has a few integer properties on top of the inherited UIView ones and Deck has an NSMutableArray for holding some cards. After generating a deck of cards, I want to display a randomly selected card (by adding it to the superview). Before I do, I check to see if there is a card already, I call a method to release it before asking for a new one. But I get the warning in the title. Here's the code...


#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];
    }

I am a beginner at programming (other than Basic!) so I'm still wrapping my head around the whole object thing. Thanks for any help and/or advice!

EDIT: Here's the Card and Deck code...


#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:fr开发者_JS百科ame])) {
    // 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


You've defined the fadeAway method for the FlashTestViewController class, not the Card class. That means that you can only call this method (or send the message depending on your preferred OOP terminology) on instances of the Card class.

so [aCard fadeAway] is incorrect because it takes the wrong number of params, but also because aCard is a Card class instance and the fadeAway: method isn't defined for that class (well, we don't see the definition of it, so maybe it is but not visibly so).

But you didn't show the definition of the Card class so maybe you DO define the method there.


You need to give aCard an (id)sender as you stated in your parameters. Your not giving enough arguments to put it in other words.


You declared fadeAway as taking a single parameter of type id, so you have to call it that way. So:

[aCard fadeAway:nil];

Also, since it never does anything with its argument, you could simply declare it as not taking one, and then call it as is. So:

-(void)fadeAway;

// later
[aCard fadeAway];
0

精彩评论

暂无评论...
验证码 换一张
取 消