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

  • Home
  • SEARCH
  • 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 1093041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:46:18+00:00 2026-05-16T23:46:18+00:00

I want to create a basic app where you have 8 numbered buttons, with

  • 0

I want to create a basic app where you have 8 numbered buttons, with a randomly generated number on each. You are given a total value, and have to click on buttons whose numbers add up to that value.

I need to show which buttons have been ‘used’ already (I hope to use the selected state for that, as I am using a custom button).

When you press a button, it needs to add the correct value to the ‘totalSoFar and change the button to it’s selected state.

The plan is that I currently have 8 UIButtons (b1, b2 etc). I have generated a random array of numbers, that I want to assign as the title of the button.
I have tried about a million variations on the theme of [b1 setTitle etc] but can’t get it to work.
I have added a textview on top of the button (n1, n2…) that now displays my random numbers instead.
I have got it so that when you press a button, it gets the tag of that button, and then finds the matching position in the array, so I can use that value in the ‘what’s the total of all the buttons I have pressed’ calculation. I cannot however figure out how to access the sender button in order to tell it to change to selected.
I’d much rather do this by just changing the title of the button, rather than having to have an extra textfield.

I am still quite new to Objective C, having previously only coded in actionscript for Flash, and I’m only asking after spending about 4 hours trying to search for a solution online (and swearing at my computer a lot…)

Here’s what I have so far… I have tagged each button, so that b1 has a tag of 0, b2 has a tag of 1 etc…

-(IBAction) pressed:(id)sender {

int i;
UIButton *button = (UIButton *)sender;
for (i=0; i<8; i++) {
    if ([button tag] == i)  
    {
        //returns value in array as a string
        NSString *temp2 = [pnumbers objectAtIndex:i];
        //checks string value to see what total to add
        if ([temp2 isEqualToString:@"1"]) {
            total +=1;
        }
        if ([temp2 isEqualToString:@"2"]) {
            total +=2;
        }
        if ([temp2 isEqualToString:@"3"]) {
            total +=3;
        }
        if ([temp2 isEqualToString:@"4"]) {
            total +=4;
        }
        if ([temp2 isEqualToString:@"5"]) {
            total +=5;
        }
        if ([temp2 isEqualToString:@"6"]) {
            total +=6;
        }
        if ([temp2 isEqualToString:@"7"]) {
            total +=7;
        }
        if ([temp2 isEqualToString:@"8"]) {
            total +=8;
        }
        if ([temp2 isEqualToString:@"9"]) {
            total +=9;
        }
}
}

}

I don’t want to just make a new function for each separate button (as I plan to have 64 buttons in the end) in order to be able to access which button it was without lots of if and for statements. If nothing else, that seems like bad coding to me!
Hope this all makes sense…
I mostly just want to make the damn thing show the selectedState for the correct button once I’ve clicked on it. I can live with the textfields!

EDIT – for the record, I’ve now managed to get it to the selected state, by using
[b1 setSelected:FALSE];

at the start, and then adding

UIButton *button = (UIButton *)sender;
[sender setSelected:TRUE];

before the for loop. Still not sure how to get it to deselect the next time it is clicked though.

  • 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-16T23:46:19+00:00Added an answer on May 16, 2026 at 11:46 pm

    It seems like your code could be simplified:

    - (IBAction) pressed:(UIButton *)sender {
      int buttonIndex = [button tag];
      total += [[pnumbers objectAtIndex:buttonIndex] intValue];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a snippet to create a 'Like' button for our news site: <iframe
I want the messagebox to only show if the number is equal to 0.
I want to have generalised email templates. Currently I have multiple email templates with
If all tables I want to delete from have the column gamer_id can i
I want to use a temp directory that will be unique to this build.
I want to get the header of a selected tab-item of a tab-control and
Every time that I want to do a Layout, I'm getting a black layout
I'm working on a project in C# w/ XNA, and I want to reorganize
I'm in the process of porting some code from Linux to Mac OS X.
I'm trying to build a Chrome browser extension, that should enhance the way the

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.