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.
You could process all of the button presses through the same method, where
(id)senderis the argument value that is passed. Then you can do your button-specific processing by comparing (with a cast) the value of(UIButton*)senderto 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 likelastButtonPressed = 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: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: