模拟按下iphone中方法调用中的按钮

时间:2021-12-17 21:33:13

I am working on a little calculator program and I am trying to add a variable button. What I would like to do is that if the user presses the variable button I would like to automatically multiply this variable by whatever numbers were typed before. I have the method:

我正在研究一个小计算器程序,我正在尝试添加一个可变按钮。我想要做的是,如果用户按下变量按钮,我想自动将此变量乘以之前输入的数字。我有方法:

- (IBAction)operationPressed:(UIButton *)sender

and I have a button with a * symbol on it. Now, within the variable pressed method

我有一个带*符号的按钮。现在,在变量压缩方法中

- (IBAction)variablePressed:(UIButton *)sender

I would like to simulate the pressing of the times button (*). So I have the following code for example

我想模拟按下时间按钮(*)。所以我有以下代码例如

- (IBAction)variablePressed:(UIButton *)sender{
if (self.userIsInTheMiddleOfEnteringANumber) {
    [self operationPressed:<how to simulate pressing *???>];
}

Thanks in advance

提前致谢

UPDATE after comments

评论后更新

It is an RPN calculator so you can put in the list of numbers as a stack and then press the operation buttons and it will do the operations on the stack from the top to the bottom. The operationPressed method:

它是一个RPN计算器,因此您可以将数字列表作为堆栈放入,然后按下操作按钮,它将从顶部到底部对堆栈执行操作。 operationPressed方法:

- (IBAction)operationPressed:(UIButton *)sender
{
    if (self.userIsInTheMiddleOfEnteringANumber) [self enterPressed];
    double result = [self.brain performOperation:sender.currentTitle];

    self.display.text = [NSString stringWithFormat:@"%g", result];
    self.history.text = [self.history.text stringByAppendingString:sender.currentTitle];
}

so when an operation is pressed it is send to the performOperation method which then decides which operation to do. My solution (including putting in some variable values for testing is the following:

所以当按下一个操作时,它会发送到performOperation方法,然后该方法决定要做哪个操作。我的解决方案(包括为测试添加一些变量值如下:

- (IBAction)variablePressed:(UIButton *)sender
{
    NSString * variable = sender.currentTitle;
    NSDictionary *testDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"4",@"x",@"10",@"y",@"2",@"z", nil];
    if (self.userIsInTheMiddleOfEnteringANumber) {
        [self enterPressed];
    }
    id numberForVariable = [testDictionary objectForKey:variable];

    [self.brain pushOperand:[numberForVariable doubleValue]];
    self.userIsInTheMiddleOfEnteringANumber = NO;
    self.history.text = [self.history.text stringByAppendingString:variable];
    //this is working pretty good. Now i need to make sure it works when i run program given values for variables.

}

4 个解决方案

#1


1  

Try this: [button sendActionsForControlEvents:UIControlEventTouchUpInside]; where button refers to your 'times button'.

试试这个:[button sendActionsForControlEvents:UIControlEventTouchUpInside];按钮指的是“时间按钮”。

#2


0  

So the operationPressed message checks what the sender is to decide what operation to perform? I think that's probably not a good design.

所以operationPressed消息检查发送者决定要执行什么操作?我认为这可能不是一个好的设计。

But, can you look at the implementation of operationPressed and see what it does if the button is the * button? Instead of calling operationPressed you should just do what operationPressed does in the case you want.

但是,你能看看operationPressed的实现,看看如果按钮是*按钮它会做什么?而不是调用operationPressed,你应该只在你想要的情况下执行operationPressed所做的事情。

Alternatively, if you want to pursue this path and just get ahold of the right button, operationPressed must have a way to access the buttons for comparison, right? You should be able to access the button you need in the same way.

或者,如果你想要追求这条路径并且只是得到正确的按钮,那么operationPressed必须有办法访问按钮进行比较,对吗?您应该能够以相同的方式访问所需的按钮。

#3


0  

assuming OperationPressed takes a UIButton can't you simply say

假设OperationPressed采用UIButton,你不能简单地说

 [self operationPressed:sender];

#4


0  

this is how you can do it just create an object of the *(star) button, then in the Variable pressed methood

这就是你如何才能创建*(星号)按钮的对象,然后在变量压缩的方法中

IBOutlet UIButton *starBtn_Obj;

- (IBAction)variablePressed:(UIButton *)sender{
if (self.userIsInTheMiddleOfEnteringANumber) {
[self operationPressed:starBtn_Obj];
}

this will exectue the operationPressed methood as if the *(star button was pressed).

这将解释operationPressed methood,好像*(按下星形按钮)。

#1


1  

Try this: [button sendActionsForControlEvents:UIControlEventTouchUpInside]; where button refers to your 'times button'.

试试这个:[button sendActionsForControlEvents:UIControlEventTouchUpInside];按钮指的是“时间按钮”。

#2


0  

So the operationPressed message checks what the sender is to decide what operation to perform? I think that's probably not a good design.

所以operationPressed消息检查发送者决定要执行什么操作?我认为这可能不是一个好的设计。

But, can you look at the implementation of operationPressed and see what it does if the button is the * button? Instead of calling operationPressed you should just do what operationPressed does in the case you want.

但是,你能看看operationPressed的实现,看看如果按钮是*按钮它会做什么?而不是调用operationPressed,你应该只在你想要的情况下执行operationPressed所做的事情。

Alternatively, if you want to pursue this path and just get ahold of the right button, operationPressed must have a way to access the buttons for comparison, right? You should be able to access the button you need in the same way.

或者,如果你想要追求这条路径并且只是得到正确的按钮,那么operationPressed必须有办法访问按钮进行比较,对吗?您应该能够以相同的方式访问所需的按钮。

#3


0  

assuming OperationPressed takes a UIButton can't you simply say

假设OperationPressed采用UIButton,你不能简单地说

 [self operationPressed:sender];

#4


0  

this is how you can do it just create an object of the *(star) button, then in the Variable pressed methood

这就是你如何才能创建*(星号)按钮的对象,然后在变量压缩的方法中

IBOutlet UIButton *starBtn_Obj;

- (IBAction)variablePressed:(UIButton *)sender{
if (self.userIsInTheMiddleOfEnteringANumber) {
[self operationPressed:starBtn_Obj];
}

this will exectue the operationPressed methood as if the *(star button was pressed).

这将解释operationPressed methood,好像*(按下星形按钮)。