Register    Login    Forum    Search    FAQ

Board index » Programming » Objective-C / iPhone Development

 Page 1 of 1 [ 2 posts ] 

Author Message
 Post subject: how to get random array value through arc 4 random
 Post Posted: Thu Jan 31, 2013 1:53 am 

Joined: Thu Jan 31, 2013 1:41 am
Posts: 1
Languages: objective c
Text editor: word
hello i am struggling but not success on one of my app i am facing problem ...
this is the app for the kids in that its just like a identification game means
i have 36 image , 36 image text and 36 sound now these 3 arrays are there
now i want randomly 9 images , sound and text and put that 9 images on button tag and
text and sound play one bye one then user have to identify the right image so for that what should
i have to please suggest me .

 Post subject: Re: how to get random array value through arc 4 random
 Post Posted: Thu Jan 31, 2013 11:14 am 
Forum Veteran
Forum Veteran

Joined: Wed May 30, 2012 11:11 am
Posts: 131
Languages: Objective-C, html, xhtml, css
Text editor: xcode, notepad++, Joomla
Well, first you need to declare a mutable array and synthesise it. Then you need a butten was clicked method, looking something like this:
objc code:
- (IBAction)buttonWasClicked:(id)sender {
    int size = [usedNumbers count];
    if (size > 0) {
        int position = (arc4random() % size);
        // Finally, remove it from the array:
        [usedNumbers removeObjectAtIndex:position];
    } else {
        // The array is empty.

then you need to take care of the NIB like this
objc code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Create two arrays and make the pointers point to them
        TheArray = [NSMutableArray array];
    return self;

then you need a output method for your text label, looking something like this :)
objc code:
- (IBAction)TextFieldMethod:(id)sender
    int sizeSomething = [TheArray count];
    int position2 = (arc4random() % sizeSomething);
    // Step to the next question
    NSString *randomText = [TheArray objectAtIndex:position2];
    // Am I past the last question?
    // Get the string in the current index of the questions array
    // Output the question string to the debug console
    // Display the string in the question text field
    [TextField setText:randomText];

Hope this helped, :)

PS. you can't just copy the code directly, you most likely have to personalise it a bit for your own use, but I don't have you project so i tried to generalise it for you :)

League of Legends Counter Picks FREE, my second iPhone app :) ... 52912?mt=8

Display posts from previous:  Sort by  
 Page 1 of 1 [ 2 posts ] 

Board index » Programming » Objective-C / iPhone Development

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: