随机数以拾取图像Xcode iPhone
我一直在网上搜寻这个问题的答案!希望你们中的一个能帮助我 到目前为止,我有一个按钮,可以在标签中生成1到6之间的随机nr 然后我希望根据标签中的nr显示图片 例如:如果生成了数字1,我希望img1.png显示在UIImageView中。(?) 我在想也许可以用if函数来选择图片 对不起,格式化不好 以下是.h文件:随机数以拾取图像Xcode iPhone,xcode,random,Xcode,Random,我一直在网上搜寻这个问题的答案!希望你们中的一个能帮助我 到目前为止,我有一个按钮,可以在标签中生成1到6之间的随机nr 然后我希望根据标签中的nr显示图片 例如:如果生成了数字1,我希望img1.png显示在UIImageView中。(?) 我在想也许可以用if函数来选择图片 对不起,格式化不好 以下是.h文件: #import <UIKit/UIKit.h> @interface xxxViewController : UIViewController { IBOut
#import <UIKit/UIKit.h>
@interface xxxViewController : UIViewController
{
IBOutlet UILabel *label;
int randomNumber;
}
-(IBAction)randomize;
@end
您可以使用该编号创建文件名,例如
[NSString stringWithFormat:@“image%d.png”,number]代码>。创建一个文件名列表并将它们与NSDictionary中的数字相关联可能是一种更好的做法(尽管这对您所做的工作不是必需的),这样您就可以始终处理已知的图像名。我理解您的意思,但我对Objective C有点陌生。您是否可以在代码中实现您的第一个备选方案?那太好了!
#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