跟踪最后按下的特定类型按钮的方法
我有一个简单的计算器应用程序。我有 4 个操作按钮和计算器数字。我想跟踪最近按下的操作员按钮。
当按下任何操作按钮时,其标签颜色从白色变为橙色。然后,当按下数字按钮时,标签将恢复为白色。我想要做的是,如果用户在按另一个运算符或等于之前按清除,则标签会变回橙色。
我的想法是在clearDisplay方法内部有一个if语句,例如: 如果 (lastOperatorPressed == 按钮 1) { 更改其标题颜色...
因此,如果用户输入一系列数字然后点击 +,则 + 按钮将被注册为最近按下的操作符按钮,然后如果他们继续输入下一系列数字并在按下另一个操作符或等于之前按clear,我在clearDisplay内的if语句将被触发。
我无法找到一种方法来跟踪最近按下的操作员按钮。
非常感谢任何帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过相同的方法处理所有按钮按下操作,其中
(id)sender
是传递的参数值。然后,您可以通过将(UIButton*)sender
的值与特定按钮对象值进行比较(通过强制转换)来执行特定于按钮的处理。在这个方法中,现在您始终拥有 sender 的值,它是最后按下的按钮。将其分配给像lastButtonPressed = sender;
这样的变量更新:抱歉,应该像
lastButtonPressed = (UIButton*)sender;
以下是更多详细信息:
如果您使用 Interface构建器,将每个按钮连接到视图控制器中的按钮处理程序方法。 (我不太使用 Interface Builder,但我认为您想将 touch-up inside 事件连接到按钮处理程序方法)。
如果您不使用 Interface Builder,则可以在视图控制器中初始化 ivars 的位置添加此行。假设您的按钮处理程序称为
buttonHandler:
对每个按钮执行此操作:在头文件中,创建一个名为
lastButtonPressed
的实例变量(例如)。在初始化其他 ivars 的实现文件中将其初始化为 nil。然后添加一个这样的方法:
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 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: