出于学习目的,我正在 iphone 上构建计算器。
我遇到这样一种情况,我有多个按钮是操作(+、-、/)和多个按钮是操作数,例如1,2,3 等
因此所有操作都连接到我的 viewController 中的 operationPressed 方法。
除了在 View 中为按钮使用文本之外,在 UI 构建器中是否有其他方法来识别每个 UIButton?
什么是最佳实践?
更新
我可以在 UI 构建器中设置的身份标签呢?我可以引用吗?
我问的原因是我想识别按钮但是说'/'但按钮上的文本将是'÷'所以这就是为什么我需要一些其他字符串标识符最终可以作为参数传递给我模型。 http://cl.ly/3H1j032f1M1d3A2Y3501
最佳答案
通常处理这种情况的方法是将每个按钮的 tag
属性设置为某个唯一的整数值,并编写采用 sender
参数的操作方法,例如:
- (void)operationPressed:(id)sender
{
// cast the argument to a more specific type
UIButton *button = (UIButton *) sender;
// get the view's tag
NSInteger tag = [button tag];
// Do whatever it was you were going to do...
}
编辑
这是 UIView.h
中 tag
属性的声明:
@property(nonatomic) NSInteger tag; // default is 0
关于objective-c - 如何在代码中引用不同的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9525873/