iPhone UIImageView设置UIImages

时间:2012-12-28 04:06:28

标签: ios uiimageview

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

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

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

故事板没有显示UIImageView,我没有得到任何编译错误

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

以下是我的代码中的一些片段

ViewController.h

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

@interface ViewController : UIViewController
{
    UIImageView *cardDisplay;
}

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


@end

ViewController.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

Card.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

加入deck.h

#import "Card.h"

@interface Deck : NSObject
{
    NSMutableArray *cards;
}

@property(nonatomic, retain)NSMutableArray *cards;

@end

Deck.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

1 个答案:

答案 0 :(得分:0)

这一行:

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

应该是:

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

您需要在IB中创建的图像视图上设置图像,而不是创建新图像。这样做不会让你在以后用计时器做你想做的事。