2010-02-12 44 views
0

我一直在尋找網絡上的所有答案這一個!希望你們中的一個能幫助我。隨機數字選擇一個圖像Xcode iPhone

到目前爲止,我有一個按鈕,它會在標籤中產生1到6之間的隨機數。

那我要的圖片,這取決於是在標籤什麼NR出現。

例如:如果產生1號,我想img1.png出現在一個UIImageView 我想也許如果函數挑選的圖片。(?)?

對不好格式化。

這裏是.h文件:

#import <UIKit/UIKit.h> 

@interface xxxViewController : UIViewController 
{ 
    IBOutlet UILabel *label; 
    int randomNumber; 
} 
-(IBAction)randomize; 

@end 

...這裏是.m文件:

#import "xxxViewController.h" 
@implementation xxxViewController 

-(IBAction)randomize 
{ 
    int randomNumber = 1+ arc4random() %(6); 
    label .text = [NSString stringWithFormat:@"%d",randomNumber]; 

} 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    int randomNumber = 1+ arc4random() %(6); 
    label .text = [NSString stringWithFormat:@"%d",randomNumber]; 

} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 

@end 

回答

2

您可以用數字來創建文件名,例如[NSString stringWithFormat:@"image%d.png", number]; 。這可能是一種更好的做法(儘管不需要做什麼)來創建文件名列表,並將它們與NSDictionary中的數字相關聯,以便始終處理已知的圖像名稱。

+0

我明白你在說什麼,但我對Objective C有點新鮮感。你能否在代碼中實現你的第一個選擇?那太好了! – user272007