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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:33:50+00:00 2026-05-27T09:33:50+00:00

I have a simple calculator app. I have 4 operator buttons and the calculator

  • 0

I have a simple calculator app. I have 4 operator buttons and the calculator number digits. I want to keep track of which operator button was the most recent one pressed.

When any operator button is pressed, its label changes color from white to orange. Then when a digit button is pressed, the label reverts back to white. What I want to do is have the label turn back to orange if the user presses clear before pressing another operator or equals.

My idea is to have an if statement inside of the clearDisplay method, for example:
If (lastOperatorPressed == button1) {
Change its titleColor…

So if the user inputs a series of numbers and then hits +, the + button will be registered as the most recent operator button pressed and then if they continue inputting the next series of numbers and before pressing another operator or equals and press clear, my if statement inside clearDisplay will trigger.

I am unable to figure out a way to keep track of which operator button was most recently pressed.

Any help greatly appreciated.

  • 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-27T09:33:51+00:00Added an answer on May 27, 2026 at 9:33 am

    You could process all of the button presses through the same method, where (id)sender is the argument value that is passed. Then you can do your button-specific processing by comparing (with a cast) the value of (UIButton*)sender to specific button object values. Within this single method, now you always have the value of sender, which is the last button pressed. Assign it to a varible like lastButtonPressed = sender;

    Update: Sorry, that should be like lastButtonPressed = (UIButton*)sender;

    Here are some more details:

    If you use Interface Builder, connect each button to a button handler method in your view controller. (I don’t use Interface Builder so much, but I think you want to connect the touch-up inside event to the button handler method).

    If you don’t use Interface Builder, then you can add this line where you initialize your ivars in the view controller. Assume your button handler is called buttonHandler: Do this for every one of your buttons:

    [oneOfYourButtons addTarget:self action:@selector(buttonHandler) forControlEvents:UIControlEventTouchUpInside];
    //... include this for each button ...
    [anotherOneOfYourButtons addTarget:self action:@selector(buttonHandler) forControlEvents:UIControlEventTouchUpInside];
    

    In your header file, create an instance variable called lastButtonPressed (for example). Initialize it to nil in your implementation file where you initialized your other ivars.

    Then add a method like this:

    - (void)buttonHandler:(UIButton*)sender{
    
        if (sender == button1) {
           //... do things for button1
           // what you do here may depend on the value of lastButtonPressed, like this, for example
            if (lastButtonPressed == someButton) {
               //... do something 
               //... you may want to reset the value of lastButtonPressed
               lastButtonPressed = nil; 
    
        }
        else if (sender == button2) {
           //... do things for button2
        }
        else if (sender == button3) {
           //... do things for button3
        }
        else
           //... Repeat for all the buttons you want to handle. The compiler will optimize this so don't worry about that
           //...
    
        // probably at the end, you want to save the current button as the last button pressed
        lastButtonPressed = sender;
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have coded my calculator in simple mode, I have added a button for
Say I want to have a simple web app that takes some user input,
I have a simple form, which performs a calculation when a digit is pressed,
I have simple web application (asp.net - ajax) I have a web form which
I have a simple tabbar app. Built directly from the tabbar template. As I
I have a simple class in a Django app called project from django.test import
All things being equal, and in the most simple form, which is faster? 1.)
I'm coding a simple calculator and I want to make an unique function for
I have an app that does a simple calculation from 2 text fields, the
I'm familiar with core data but want to make a simple app that calculates

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.