2

im new to programming and im trying to make my first iphone app. basically what my app needs to do is draw a random image from an array and output it to a UIImageView in a view. it should be really simple but i cant find anywhere on the internet how to output the array to the uiimage

theArray = [[NSArray alloc] initWithObjects:@"one.png",@"two.png",@"three.png",nil];

heres a basic code of the images. any help would be greatly appreciated!!

also if you could explain any other details i might need.

1 Answer 1

3

find a random array index using -

int randomArrIndex = arc4random() % [arr count];

then you can fetch image name from array with this index

UIImageView *imgView = [[UIImageView alloc] initWithImage:[arr objectAtIndex:randomArrIndex]];
Sign up to request clarification or add additional context in comments.

10 Comments

wow that was fast, thanks so much i will try that as soon as i get home.
actually the code you told me isnt working, its saying randomArrIndex isnt defined and [arr objectAtIndex:randomArrIndex] isnt defined
UIImageView *cardOutput = [[UIImageView alloc] initWithImage: [Array objectAtIndex:randomArrIndex]]; - (void)viewDidLoad { Array = [[NSArray alloc] initWithObjects:@"one.png",@"two.png",@"three.png",nil]; int randomArrayIndex = arc4random() % [Array count]; [super viewDidLoad]; } is randomArrIndex a method i need to define? im still confused sorry
@Saurabh is randomArrIndex a method i need to define? im still confused sorry. the book im reading to learn to program is called "sams teach yourself iphone aplication developtment in 24 hours" and it barely touches on arrays at all and google is no help.
randomArrIndex is an integer I defined in answer as - int randomArrIndex = arc4random() % [arr count]; write this line in your code before using it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.