-8

我有一个图像视图,它在视图加载时随机生成图像,我想将第二个图像视图添加到同一个视图控制器。我以为我可以复制我的代码并更新属性,但它现在可以工作了。这是我的代码:

。H

#import <UIKit/UIKit.h>

@interface ImageViewViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *imageView;

@end

.m

[super viewDidLoad];

int randomImages = rand() % 4;
switch (randomImages) {
    case 0:
        _imageView.image = [UIImage imageNamed:@"1.png"];
        break;
    case 1:
        _imageView.image = [UIImage imageNamed:@"2.png"];
        break;
    case 2:
        _imageView.image = [UIImage imageNamed:@"3.png"];
        break;
    case 3:
        _imageView.image = [UIImage imageNamed:@"4.png"];
        break;
}
4

1 回答 1

0

这适用于我的 Xcode 4.6.2。

整合来自@rmaddy 的评论。

视图控制器.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;

@end

视图控制器.m

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _imageView1.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", arc4random_uniform(4) + 1]];
    _imageView2.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", arc4random_uniform(4) + 1]];

}

@end

提示:使用 Interface Bilder 和 Assitant Editor 连接属性。希望有帮助。

于 2013-07-16T18:51:03.480 回答