【发布时间】:2012-12-28 04:06:28
【问题描述】:
我正在制作纸牌游戏并尝试从对象的实例变量中调用 UIImages 来更新 UIImageView
我有一个 Deck 对象,它有一个 Card 对象的 NSArray 实例变量。
每个 Card 对象都有一些实例变量,其中一个是我试图在 UIImageView 中显示的 UIImage ......这就是我遇到问题的地方
情节提要没有显示 UIImageView 并且我没有收到任何编译错误
我要更新的 UIImageView 是 cardDisplay (ViewController.h)
这是我的代码中的一些 sn-ps
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
卡.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
【问题讨论】:
-
您是否在情节提要中添加了图像视图并连接了插座?
-
如果您在 IBoutlet 中有 UIImageview,只需将它们链接到文件所有者即可。在您的代码中,您已在视图中初始化并加载了不需要的内容。
-
最终我将设置一个计时器并随机显示不同的卡片,所以我试图通过代码严格执行此操作,而不是通过 IB 将特定图片连接到 UIIMageView。这不是更新吗,因为我需要一个代表来与 Deck/Card 模型中的视图交谈
-
好吧,如果你想在代码中完成这一切,那么你做错了。您应用中的图像视图是在 IB 中设置的视图,但您设置图像的视图是您分配的新视图,但永远不会添加到您的视图中。
-
如果你想在代码中添加 UIIImageView 只需删除 IBOutlet
@property (nonautomic,strong) UIImageview *name;
标签: ios uiimageview