0

我正在制作纸牌游戏并尝试从对象的实例变量中调用 UIImages 来更新 UIImageView

我有一个 Deck 对象,它有一个 Card 对象的 NSArray 实例变量。

每个 Card 对象都有一些实例变量,其中一个是我试图在 UIImageView 中显示的 UIImage ......这就是我遇到问题的地方

情节提要没有显示 UIImageView 并且我没有收到任何编译错误

我要更新的 UIImageView 是 cardDisplay (ViewController.h)

这是我的代码中的一些片段

视图控制器.h

#import "Deck.h"
#import "Card.h"

@interface ViewController : UIViewController
{
    UIImageView *cardDisplay;
}

@property (nonatomic, retain) IBOutlet UIImageView *cardDisplay;


@end

视图控制器.m

    #import "ViewController.h"

    #import "Deck.h"
    #import "Card.h"

    @implementation ViewController


@synthesize cardDisplay;

- (void)viewDidLoad
{
    [super viewDidLoad];

    Deck *deck = [[Deck alloc]init];

    NSLog(@"%@", deck);

    for (id cards in deck.cards) {
         NSLog(@"%@", cards);
    }

    self.cardDisplay = [[UIImageView alloc] initWithImage:
         [[deck.cards objectAtIndex:0 ] cardImage]];
}


@end

卡片.h

@interface Card : NSObject
{
    NSString *valueAsString, *suitAsString;
    NSInteger faceValue, countValue;
    Suit suit;
    UIImage *cardImage;
}

@property (nonatomic, retain) NSString *valueAsString;
@property (nonatomic, retain) NSString *suitAsString;
@property (nonatomic) NSInteger faceValue;
@property (nonatomic) NSInteger countValue;
@property (nonatomic) Suit suit;
@property (nonatomic) UIImage *cardImage;

- (id) initWithFaceValue:(NSInteger)aFaceValue countValue:(NSInteger)aCountValue
                suit:(Suit)aSuit cardImage:(UIImage*)aCardImage;

@end

甲板.h

#import "Card.h"

@interface Deck : NSObject
{
    NSMutableArray *cards;
}

@property(nonatomic, retain)NSMutableArray *cards;

@end

甲板.m

#import "Deck.h"
#import "Card.h"

@implementation Deck

@synthesize cards;

- (id) init
{
    if(self = [super init])
    {
        cards = [[NSMutableArray alloc] init];

        NSInteger aCount, picNum = 0;

        for(int suit = 0; suit < 4; suit++)
        {
            for(int face = 1; face < 14; face++, picNum++)
            {

                if (face > 1 && face < 7) 
                    aCount = 1;
                else if (face > 6 && face < 10)
                    aCount = 0;
                else
                    aCount = -1;

                NSString *path = [[NSBundle mainBundle] bundlePath];
                NSString *imagePath = [path stringByAppendingPathComponent: 
                       [NSString stringWithFormat:@"/cards/card_%d.png",picNum]];

                UIImage *output = [UIImage imageNamed:imagePath];

                Card *card = [[Card alloc] initWithFaceValue:(NSInteger)face
                                                countValue:(NSInteger)aCount
                                                suit:(Suit)suit
                                                cardImage:(UIImage *)output];

                [cards addObject:card];
            }

        }
    }
    return self;
}


@end
4

1 回答 1

0

这一行:

self.cardDisplay = [[UIImageView alloc] initWithImage:
         [[deck.cards objectAtIndex:0 ] cardImage]];

应该:

self.cardDisplay.image = [[deck.cards objectAtIndex:0 ] cardImage];

您需要在 IB 中创建的图像视图上设置图像,而不是创建一个新的。这样做并不会阻止您以后使用计时器做您想做的事情。

于 2012-12-28T04:31:27.150 回答