Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 770745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:32:47+00:00 2026-05-14T18:32:47+00:00

I’m making a little quiz-style application but I’ve got a few issues. I random

  • 0

I’m making a little quiz-style application but I’ve got a few issues.

I random the questions from a NSMutableArray using arc4random(), then I populate the view with 3 buttons, one which includes a correct answer and the other 2 include two wrong answers

what I need to do is to randomize the X coordinate (position) of the 3 buttons in the view this is the code I’m using, but it gives me problems as it doesn’t work properly nd the app often crashes when calling the action:

NSBundle *bundle02 = [NSBundle mainBundle];
NSString *textFilePath02 = [bundle02 pathForResource:@"possiblePositions" ofType:@"txt"];   
NSString *fileContents02 = [NSString stringWithContentsOfFile:textFilePath02 encoding:NSUTF8StringEncoding error:nil];
arrayPossiblePositions = [[NSMutableArray alloc] initWithArray:[fileContents02 componentsSeparatedByString:@"\n"]];

int length02 = [arrayPossiblePositions count];
int chosen02 = arc4random() % length02;
[arrayPossiblePositions removeObjectAtIndex:chosen02];
int chosen04 = arc4random() % length02;
[arrayPossiblePositions removeObjectAtIndex:chosen04];
int chosen05 = arc4random() % length02;

if ([questionString isEqualToString:@"question1"]) {

    buttonCorrect = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect newSize = CGRectMake(chosen02, 80, 130, 130);
    buttonCorrect.frame = newSize;
    [buttonCorrect setImage:[UIImage imageNamed:@"kncpf.png"] forState:UIControlStateNormal];
    [buttonCorrect addTarget:self action:@selector(answerCorrect) forControlEvents:UIControlEventTouchUpInside];
    [main addSubview:buttonCorrect];

    buttonUncorrect01 = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect newSize02 = CGRectMake(chosen04, 80, 130, 130);
    buttonUncorrect01.frame = newSize02;
    [buttonUncorrect01 setImage:[UIImage imageNamed:@"kncpf02.png"] forState:UIControlStateNormal];
    [buttonUncorrect01 addTarget:self action:@selector(answerUncorrect) forControlEvents:UIControlEventTouchUpInside];
    [main addSubview:buttonUncorrect01];

    buttonUncorrect02 = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect newSize034578 = CGRectMake(chosen05, 80, 130, 130);
    buttonUncorrect02.frame = newSize034578;
    [buttonUncorrect02 setImage:[UIImage imageNamed:@"kncpf034578.png"] forState:UIControlStateNormal];
    [buttonUncorrect02 addTarget:self action:@selector(answerUncorrect) forControlEvents:UIControlEventTouchUpInside];
    [main addSubview:buttonUncorrect02];
}

can you suggest me doing something different, because I’m really getting crazy ?

Thanks in advance for answers,
David

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:32:47+00:00Added an answer on May 14, 2026 at 6:32 pm

    I actually needed to do something similar, but instead of moving the images around, I decided instead to do this:

    • Create three buttons where you want them to appear (predetermined locations).
    • Assign the images to each button randomly (by randomizing a NSMutableArray with the NSStrings of the image names).
    • Instead of assigning @selector(answerCorrect) and @selector(answerUncorrect), assign @selector(checkIfCorrect:)
    • Define checkIfCorrect as such:

    -(void)checkIfCorrect:(id)sender{ UIImage *buttonImage = sender.image;

    if(buttonImage == [UIImage
    imageNamed:@”kncpf.png”]){ [self
    answerCorrect]; } else { [self
    answerIncorrect]; }

    }

    EDITED TO INCLUDE THE CODE I RECOMMEND:

    Also, you are calling

    int length02 = [arrayPossiblePositions count];
    int chosen02 = arc4random() % length02;
    [arrayPossiblePositions removeObjectAtIndex:chosen02];
    int chosen04 = arc4random() % length02;
    [arrayPossiblePositions removeObjectAtIndex:chosen04];
    int chosen05 = arc4random() % length02;
    

    Notice that length02 remains the same, while the size of arrayPossiblePositions changes. This is probably the first reason why your code crashes: you are trying to remove an index from an array which is outside of the array count!

    I have not tested, but should work: (dont forget to define checkanswer() as I mentioned above)

    NSMutableArray *imagesArray = [NSMutableArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil];
    
    int count1 = [imagesArray count];
    int index1 = arc4random() % count1;
    
    button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect newSize = CGRectMake(30, 80, 130, 130);
        button1.frame = newSize;
        [button1 setImage:[UIImage imageNamed:[imagesArray objectAtIndex:index1]] forState:UIControlStateNormal];
        [button1 addTarget:self action:@selector(checkAnswer:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button1];
    
    [imagesArray removeObjectAtIndex:index1];
    int count2 = [imagesArray count];
    int index2 = arc4random() % count2;
    
    button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect newSize = CGRectMake(160, 80, 130, 130);
        button2.frame = newSize;
        [button2 setImage:[UIImage imageNamed:[imagesArray objectAtIndex:index2]] forState:UIControlStateNormal];
        [button2 addTarget:self action:@selector(checkAnswer:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button2];
    
    [imagesArray removeObjectAtIndex:index2];
    int count3 = [imagesArray count];
    int index3 = arc4random() % count3;
    
    button3 = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect newSize = CGRectMake(290, 80, 130, 130);
        button3.frame = newSize;
        [button3 setImage:[UIImage imageNamed:[imagesArray objectAtIndex:index3]] forState:UIControlStateNormal];
        [button3 addTarget:self action:@selector(checkAnswer:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button3];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.