0

我有一个带有 10 个 IBOutlet 图像的 UIViewController,我有一个图像,我想根据我的 i 在 for 循环中显示它我的意思是如果我的 i 等于 3 则将 img1、img2、img3 放在视图中

这是我的代码,我的问题是我无法显示它,请你告诉我是什么问题,提前谢谢!

@property (strong, nonatomic) IBOutlet UIImageView *img1;
@property (strong, nonatomic) IBOutlet UIImageView *img2;
@property (strong, nonatomic) IBOutlet UIImageView *img3;
@property (strong, nonatomic) IBOutlet UIImageView *img4;



- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];

NSLog(@"Card description : %@", [_card description]);

int stampNumber = [_card.stampNumber intValue];

int i = 0;
UIImage *stampIMG = [UIImage imageNamed:@"1.jpg"];

NSLog(@"StampNumber  :  %i", stampNumber);
for (i = 0; i < stampNumber; i++) {
    NSLog(@"i = %i", i);
    switch (i)
    {
        case 0:
            [_img1 setImage:stampIMG];
            NSLog (@"one");
            break;
        case 1:
            [_img2 setImage:stampIMG];
            NSLog (@"two");
            break;
        case 2:
            [_img3 setImage:stampIMG];
            NSLog (@"three");
            break;
        case 3:
            [_img4 setImage:stampIMG];
            NSLog (@"four");
            break;
        case 4:
            [_img5 setImage:stampIMG];
            NSLog (@"five");
            break;
        case 5:
            [_img6 setImage:stampIMG];
            NSLog (@"six");
            break;
        case 6:
            [_img7 setImage:stampIMG];
            NSLog (@"seven");
            break;
        case 7:
            [_img8 setImage:stampIMG];
            NSLog (@"eight");
            break;
        case 8:
            [_img9 setImage:stampIMG];
            NSLog (@"nine");
            break;
        case 9:
            [_img10 setImage:stampIMG];
            NSLog (@"ten");
            break;
        default:
            NSLog (@"test");
            break;
    }
        }

}

这是我的日志:

StampNumber  :  5
2013-03-18 14:14:31.  i = 0
2013-03-18 14:14:31.  one
2013-03-18 14:14:31.  i = 1
2013-03-18 14:14:31.  two
2013-03-18 14:14:31.  i = 2
2013-03-18 14:14:31.  three
2013-03-18 14:14:31.  i = 3
2013-03-18 14:14:31.  four
2013-03-18 14:14:31.  i = 4
2013-03-18 14:14:31.  five
4

3 回答 3

0

你需要使用:

UIImage *stampIMG = [UIImage imageNamed:@"1"];

文件扩展名没有给出imageNamed:

于 2013-03-18T18:24:15.050 回答
0

您的代码似乎存在一些问题:

- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];

NSLog(@"Card description : %@", [_card description]);

int stampNumber = [_card.stampNumber intValue];

int i = 0;

/* Like Anoop pointed out, you do need an extension for the image */
UIImage *stampIMG = [UIImage imageNamed:@"1"];

NSLog(@"StampNumber  :  %i", stampNumber);
for (i = 0; i < stampNumber; i++) {
    NSLog(@"i = %i", i);

/* One more point here is although this is easy for 10 images to do a manual check and update (and efficient in this case), its better to use an IBOutletCollection with tag indication if you need.
*/
    switch (i)
    {
        case 0:
            [_img1 setImage:stampIMG];
            NSLog (@"one");
            break;
        case 1:
            [_img2 setImage:stampIMG];
            NSLog (@"two");
            break;
        case 2:
            [_img3 setImage:stampIMG];
            NSLog (@"three");
            break;
        case 3:
            [_img4 setImage:stampIMG];
            NSLog (@"four");
            break;
        case 4:
            [_img5 setImage:stampIMG];
            NSLog (@"five");
            break;
        case 5:
            [_img6 setImage:stampIMG];
            NSLog (@"six");
            break;
        case 6:
            [_img7 setImage:stampIMG];
            NSLog (@"seven");
            break;
        case 7:
            [_img8 setImage:stampIMG];
            NSLog (@"eight");
            break;
        case 8:
            [_img9 setImage:stampIMG];
            NSLog (@"nine");
            break;
        case 9:
            [_img10 setImage:stampIMG];
            NSLog (@"ten");
            break;
        default:
            NSLog (@"test");
            break;
    }
        }


/*
are the _imgi IBOutlets reference outlets from either your story board or xib file?
if so you do not need to add these as subviews.
If not so, then these are supposed to be instantiated in your code and they need to be UIImageView's rather than IBOutlets. IBOutlets are nothing but void type casts just for the compilers efficiency.
*/

    [self.view addSubview:_img1];
    [self.view addSubview:_img2];
    [self.view addSubview:_img3];
    [self.view addSubview:_img4];
    [self.view addSubview:_img5];
    [self.view addSubview:_img6];
    [self.view addSubview:_img7];
    [self.view addSubview:_img8];
    [self.view addSubview:_img9];
    [self.view addSubview:_img10];
}
于 2013-03-18T18:53:51.793 回答
0

我认为您的问题出在代码的其他地方。

我有一个带有 10 个 IBOutlet 图像的 UIViewController

您已经声明了十个 IBOutlet 图像视图- 像这样?

@property (weak, nonatomic) IBOutlet UIImageView *img1;

ETC...?

然后...您已将这些声明链接到您的故事板/xib 中的十个图像视图?

如果是这样......你为什么要这样做:

[self.view addSubview:_img1];
[self.view addSubview:_img2];
[self.view addSubview:_img3];

子视图已添加到情节提要中。

如果您还没有添加视图,那么这就是您的问题所在。

例如,如果您分配/初始化每个视图,例如:

   self.img1 = [[UIImageView alloc] initWithFrame:(CGRect){10,10,50,50}];

您将看不到任何图像,因为您的 IBOutlet 是一个 WEAK 属性(它假定该对象已存在于情节提要中,因此不声明所有权)。

检查一下并整理一下...

  • 在情节提要中添加 imageViews
  • 将它们链接到您的 IBOutlets
  • addSubview

然后它应该工作

或者

  • 不要在情节提要中添加子视图
  • 将它们声明为 STRONG(非 IBOutlet)属性

    @property (strong, nonatomic) UIImageView *img1;  
    
  • 在代码中分配/初始化它们

  • addSubview

这也应该有效

于 2013-03-18T18:56:25.203 回答