views:

66

answers:

4

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: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
A: 

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.

thyrgle
+1  A: 

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];
Jason Coco
+3  A: 

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.

Dad
I don't have a fadeAway method in the Card class. I still get confused by where different bits of functionality should go. I thought that a method for dismissing a card should go in the controller, but maybe it should be in the Card class. I'll post my Card and Deck code as an answer.
Steve
Actually - I'll edit the original question as the friendly web site is suggesting to me... =)
Steve
I put the fadeAway method into the Card class without a sender and - voila! it works! When I build the deck of cards, I used random numbers to make the x and y position of the frame, and each time you tap the New Card button, the old card disappears and the new one appears somewhere else. Now to actually make it fade, and to get the content into the UIView.Thanks so much for the help!!! I'd vote up the answers, but I don't have enough reputation yet.
Steve
@Steve: " *I still get confused by where different bits of functionality should go* ". Apple pushes the model-view-controller pattern for GUI applications. If you want to follow the approved pattern, you would separate the code to do with rendering the card on the screen e.g. fading away and the code to do with the logical abstraction of a playing card into separate classes. You'd have a Card class (model) and a CardView class (view). -fadeAway would go in CardView.
JeremyP
I see - I've been trying to understand the MVC thing but it's taking time to sink in. I never thought to split the code for the card into two parts. I've been thinking about a "card object" and I figured that "card object" had to be MV or C, but not M and V both. This is hard stuff, but fun. Thanks again for the guidance - I'd have given up without it!
Steve
A: 

hey but where's the out put of all above code. I have implemented whole code still getting two warnings "card may not respond to fadeway method". And when i run it there is not display. Can you give me complete code please.

mayur
The complete code consists of a ViewController and the Deck and Card classes plus the .xib file. A lot of the view generating stuff is done in the view controller. I got some good advice to split my Card class into two parts - a model "Card" for the data of each card and a view "CardView" to handle drawing and animating the card. I could post all the code I've written, but I don't know how to post a .xib file...
Steve
It will be great help if you send me whole project code as zip file to [email protected]. Just compress your whole project and send it to me.
mayur

related questions