我想调用要在视图中显示的图像
您好,我想在视图中显示随机选择的图像。下面是我用来生成要在标签中显示的随机数的代码示例。 (它围绕着编码方式,我知道有一种更简洁的编码方法。我这样做是出于一个特定的原因,我现在不会讨论)。我希望在每种情况下将代码替换为调用要在视图中显示的图像的代码
NSString *title = nil; NSString *path = nil;
int Number = arc4random() % 12; switch(Number) { case 0: namel.text = @"1";
break;
case 1:
namel.text = @"2";
break;
case 2:
namel.text = @"3";
break;
case 3:
namel.text = @"4";
break;
case 4:
namel.text = @"5";
break;
case 5:
namel.text = @"6";
break;
case 6:
namel.text = @"7";
break;
case 7:
namel.text = @"8";
break;
case 8:
namel.text = @"9";
break;
case 9:
namel.text = @"10";
break;
case 10:
namel.text = @"11";
break;
case 11:
namel.text = @"12";
break;
break;
default:
break;
} } @end
hi I wanted to display randomly selected images in a view. below is an example of code i am using to generate random numbers to be displayed in a lable. ( its around about way to code it and im aware there is a more concise approach to code this. im doing it this way for a particular reason that i wont go into now). i wish to replace the the code with in each case to have code that calls a image to be displayed in the view
NSString *title = nil; NSString *path = nil;
int Number = arc4random() % 12; switch(Number) { case 0: namel.text = @"1";
break;
case 1:
namel.text = @"2";
break;
case 2:
namel.text = @"3";
break;
case 3:
namel.text = @"4";
break;
case 4:
namel.text = @"5";
break;
case 5:
namel.text = @"6";
break;
case 6:
namel.text = @"7";
break;
case 7:
namel.text = @"8";
break;
case 8:
namel.text = @"9";
break;
case 9:
namel.text = @"10";
break;
case 10:
namel.text = @"11";
break;
case 11:
namel.text = @"12";
break;
break;
default:
break;
} } @end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你必须使用UIImageView。然后你需要一个图像名称数组。当你随机一个数字时,只需查找数组中的索引,然后调用 imageView.image = [UIImage imagedName:[array objectAtIndex:randomIndex]];
You have to use UIImageView. Then you need an array of image name. When you random a number, just look up the index in the array and then call
imageView.image = [UIImage imagedName:[array objectAtIndex:randomIndex]];