iOS多随机图像视图

时间:2013-07-16 17:56:38

标签: ios uiimageview viewcontroller

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

·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;
}

1 个答案:

答案 0 :(得分:0)

这适用于我的Xcode 4.6.2。

综合来自@rmaddy的评论。

ViewController.h

#import <UIKit/UIKit.h>

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

@end

ViewController.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

提示:Using Interface Bilder and Assitant Editor to connect properties。 希望有所帮助。