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

I'm a Java rookie and I was wondering, if I have the following typical
Rookie question I know. I have a table with about 10 fields, one of
I am a rookie was using the Visual Studio 2008 built-in Unit Testing components,
I'm a rookie designer having a few troubles with this page: http://www.resolvegroup.co.nz/javasurvey.php There are
I've been reading through a lot of the rookie Java questions on finalize() and
My background is in WinForms programming and I'm trying to branch out a bit.
I am a fairly rookie programmer who is trying to learn the basics of
rookie here. i am using basic javascript, css to open/close div by clicking/unclicking on
Warning Rookie/newb mistakes contained within read at your own risk! Ok trying to put
I am working on some old Flex project (Flex 2.0.1 hotfix 2) and I

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.