【问题标题】:iPhone UIImageView setting UIImagesiPhone UIImageView 设置 UIImages
【发布时间】: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


【解决方案1】:

这一行:

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

应该是:

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

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

【讨论】:

  • @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] 的日志为空,您显然还有其他问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-08
  • 2023-03-12
  • 2011-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多