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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:29:37+00:00 2026-05-19T14:29:37+00:00

I am a rookie with programming. I have entered in the following code and

  • 0

I am a rookie with programming. I have entered in the following code and the app runs without bugs but crashes when the button is pressed. The goal is to identify if the button is pressed once or twice. If it is pressed a third time, it should reset to never being pressed.

buttonTestViewController.h

#import <UIKit/UIKit.h>

@interface buttonTestViewController : UIViewController {
}

-(IBAction)pressButton:(id)sender;

@end

buttonTestViewController.m

@implementation buttonTestViewController


-(IBAction)pressButton:(id)sender{
static int counter;

if (counter == 0) {
    [sender setTitle:@"not answered"];
}else if (counter == 1) {
    [sender setTitle:@"Pressed Once"];
}else if (counter == 2) {
    [sender setTitle:@"Pressed Twice"];
}
counter += 1;

if (counter > 2) {
    counter = 0;
}
}
- (void)dealloc {
[super dealloc];
}

@end

I would also like to change the background color of the button when pressed and I continue to get errors if I use setBackgroundColor. Thank you in advance for your time and consideration.

  • 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-19T14:29:37+00:00Added an answer on May 19, 2026 at 2:29 pm

    You need to initialize counter. It could be any number in the legal range of ints with how you have it right now, and it changes every time this method is called.

    static int counter = 0;
    

    Also move that line outside of your method declaration so counter isn’t reset to 0 every time the method is called. Or use an instance variable instead of a static one. That’s all you should need to get this working.

    What error do you get when you try to use setBackgroundColor:?

    Edit

    Also, if your control is a UIButton, setTitle: isn’t a valid method. Check the API docs from Apple. You’d need to do something like:

    [[sender titleLabel] setText:@"not answered"];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay this is probably a rookie question, but I have never done GUI programming
I am a true rookie in JS, I have this code, in the body
I am a rookie in iOS programming. In my test app I'd like to
This may be a rookie question but if I have created a suite of
Forgive my ignorance. I'm a rookie trying to learn some programming. I have the
This might be a really rookie question, but I'm writing an Android app that
I'm looking to learn Ruby, I have some basic programming experience but would still
I'm a Java rookie and I was wondering, if I have the following typical
So I feel like a serious rookie right now, but I have a problem
This is probably a rookie question but; Let's say I have an ActionResult that

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.