Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios iPhone UIImageView设置UIImages_Ios_Uiimageview - Fatal编程技术网

Ios iPhone UIImageView设置UIImages

Ios iPhone UIImageView设置UIImages,ios,uiimageview,Ios,Uiimageview,我正在做一个纸牌游戏,并试图从对象的实例变量调用UIImages来更新UIImageView 我有一个Deck对象,它有一个卡对象的NSArray实例变量 每个Card对象都有几个实例变量,其中一个是我试图在UIImageView中显示的UIImage…这就是我遇到的问题所在 故事板没有显示UIImageView,我没有收到任何编译错误 我尝试更新的UIImageView是cardDisplay(ViewController.h) 下面是我代码中的一些片段 ViewController.h #i

我正在做一个纸牌游戏,并试图从对象的实例变量调用UIImages来更新UIImageView

我有一个Deck对象,它有一个卡对象的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
卡片

@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
甲板

#import "Card.h"

@interface Deck : NSObject
{
    NSMutableArray *cards;
}

@property(nonatomic, retain)NSMutableArray *cards;

@end
甲板

#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
#导入“Deck.h”
#导入“Card.h”
@实现平台
@合成卡片;
-(id)init
{
if(self=[super init])
{
卡片=[[NSMutableArray alloc]init];
NSInteger aCount,picNum=0;
对于(int suit=0;suit<4;suit++)
{
用于(int face=1;face<14;face++,picNum++)
{
如果(面>1和面<7)
a数量=1;
否则如果(面>6和面<10)
a账户=0;
其他的
a数量=-1;
NSString*path=[[NSBundle mainBundle]bundlePath];
NSString*imagePath=[path stringByAppendingPathComponent:
[NSString stringWithFormat:@“/cards/card_%d.png”,picNum]];
UIImage*输出=[UIImage ImageName:imagePath];
卡*卡=[[Card alloc]initWithFaceValue:(NSInteger)面
countValue:(NSInteger)一个帐户
西服
cardImage:(UIImage*)输出];
[卡添加对象:卡];
}
}
}
回归自我;
}
@结束
此行:

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

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

您需要在IB中创建的图像视图上设置图像,而不是创建新图像。这样做并不会阻止您以后对计时器执行您想要的操作。

您是否在情节提要中添加了图像视图并连接了插座?如果您在IBoutlet中有UIImageview,请将它们链接到文件所有者。在您的代码中,您在视图中初始化了不需要的加载。最终,我将设置一个计时器并显示随机不同的卡,所以我试图严格按照代码来完成这项工作,而不是通过IB将特定图片连接到UIIMageView。这是否不更新,因为我需要一名代表从卡组/卡模型与视图对话。如果你想用代码来完成这项工作,那么你的做法是错误的。您应用程序中的图像视图是在IB中设置的,但您设置图像的视图是一个新视图,您可以将其初始分配,但从不添加到视图中。如果您想在代码中添加UIIImageView,只需删除IBOutlet
@property(非自动,强)UIImageview*name@blitzeus尝试记录self.cardDisplay和[[deck.cards objectAtIndex:0]cardImage],看看它们都给了你什么.NSLog(@“%@”,self.cardDisplay);给出错误和NSLog(@“%@,[[deck.cards objectAtIndex:0]cardImage]);我仍然不明白为什么不使用IB@blitzeus,您可以不使用IB显示图像--您可以在代码中创建UIImageView,为其提供适当的帧,并将其作为子视图添加到视图中。按照代码中的方式,您从未将其添加为视图的子视图。但是,由于[[deck.cards objectAtIndex:0]cardImage]的日志为空,您显然还有其他问题。