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 3851476
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:10:06+00:00 2026-05-19T17:10:06+00:00

Ok, so I picked up this iOS app dev project for something to work

  • 0

Ok, so I picked up this iOS app dev project for something to work on on the side, and it is materializing into something that is uglier than I was anticipating. I have a simple simple game that I have built where I present a bit of text, and the user identifies what it is, by selecting one of four buttons.

Now, I reckon a pickerview here would be a lot easier, but for design purposes, I am going to try and get my way through it doing it programmatically with buttons. So let me take a second to walk you through where I am.

When the user gets to the game screen, I have passed in two different arrays, and a number. The first array contains 25 items of text, the second array contains 25 items of answers (that correspond with the first array as far as index goes), and then the number is the number of rounds the user wishes to play, with 25 being the max.

Once the view loads, I take the number of rounds they have selected, and send it off to a method that returns an array of that number size, shuffled using the arc4random() %roundNumber method that I use as a means to access the index of the array I pass in.

This works really well. I can effectively grab at random from the source array, and after I present it, I remove the index from the index array so as to prevent duplication, and life is good.

THE PROBLEM: The buttons. I don’t know why, but I am having a real problem getting the buttons to populate randomly, and contain the correct answer as well. The main problem that I am experiencing is the fact that I can’t figure out how to randomly assign a value to the button. Meaning, the buttons have names, button1, button2, button3, and button4. And because I am programmatically setting their titles, and I need to “hardcode” the value of the correct answer into a button, it turns out that the right answer button is the same every time.

This really just boils down to pure thinking and logic, and quite frankly at this point I am running low. I am using a counter variable to access and keep track of which spot in the index is the correct answer, but I just can’t figure out the rest of the puzzle.

Below is what I have come up with…but I’m not too proud of it. I have just been retrying different things and now all I have is a mess:

    for(int i = 0; i < 4; i++){

            //digits is an array of size 4 that is populated and shuffled in a similar manner to above

            switch ([[digits objectAtIndex:i] intValue]) {
                case 0:
                    [button1 setTitle:[NSString stringWithFormat:@"%@",[self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]]forState: UIControlStateNormal];

                    NSLog(@"1 %@", [self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]);
                    break;
                case 1:
                    [button2 setTitle:[NSString stringWithFormat:@"%@",[self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]]forState: UIControlStateNormal];

                    NSLog(@"2 %@", [self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]);
                    break;
                case 2:
                    [button3 setTitle:[NSString stringWithFormat:@"%@",[self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]]forState: UIControlStateNormal];

                    NSLog(@" %@", [self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]);
                    break;
                case 3:
                                           //note this is where I hard code the correct value
                    [button4 setTitle:[self.refList objectAtIndex:counter] forState: UIControlStateNormal];

                    NSLog(@"4 %@", [self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]);
                    break;
                default:
                    break;
            }

    }

I really appreciate any help on this, and I look forward to having this mystery revealed.

Thanks!

  • 1 1 Answer
  • 1 View
  • 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-19T17:10:07+00:00Added an answer on May 19, 2026 at 5:10 pm

    Step 1 – create four buttons, set frames and background colors appropriately. Set tags on the four buttons 0, 1, 2, 3. Set all buttons to one action for touch up inside.

    Step 2 – generate a random number from 0 to 3. Use that value as an index into the array to select a button to hold the correct answer. Set the correct answer as title. Set incorrect answers on the remaining buttons. (you might use NSSet’s setWithArray to get all the buttons as a set – after removing the chosen one from the set, use allObjects to get an array of the remaining buttons)

    Step 3 – A button is pressed – what is its tag? If it matches the random number, you have a winner.

    Every UIView and subclass has a tag member. Setting the tag is easy:

    myButton.tag = 0;

    checking it is just as easy:

    if(unknownButton.tag == randomNumber)
      // winner
    

    You get all the buttons to trigger one action method so that’s where the tag is checked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a HTML5/Javascript (Sencha) app that I have packed into PhoneGap for iOS
In a project I'm working on (I picked up this code and I've been
This is some code that I picked up which I tried to implement. Its
I've been trying to make this calendar_date_picker to work for more than two days,
I am using iOS 5 to implement an app. In this app I have
In the iOS app I'm writing, we have an add photo button that's pretty
How can I implement a color picker for my app like this: http://cl.ly/8pqv into
I am trying to integrate the AddressBookUI API into my iOS 5 app to
I picked up this code from a msdn blog : #include <windows.h> #include <stdio.h>
In reference with this question We have a header with a date picked component

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.