Tun*_*vel 11 objective-c uibutton ibaction ios
我有两个UIButtons(我使用IB创建它们),它使用相同的IBAction连接到File的所有者,我如何定义哪些被按下?
Bar*_*ski 26
你的行动可以像这样实施:
- (IBAction) buttonTapped: (id) sender
// you can also replace id with UIButton*
Run Code Online (Sandbox Code Playgroud)
然后在此方法中,您可以通过-isEqual:方法进行检查
- (IBAction) buttonTapped: (id) sender
{
if ([sender isEqual:referenceToOneOfYourButtons]) {
// do something
}
else if ([sender isEqual:referenceToTheOtherButton]) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以设置不同的值来标记按钮的属性,然后:
- (IBAction) buttonTapped: (UIButton*) sender
{
const int firstButtonTag = 101;
const int otherButtonTag = 102;
if (sender.tag == firstButtonTag) {
...
}
else if (sender.tag == otherButtonTag) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
您需要在.xib或代码中设置此标记.
沿着这些方向的东西...假设button1和button2在你的头文件中.
- (IBAction)buttonPressed:(UIButton *)button {
if (button == button1) {
} else if (button == button2) {
}
}
Run Code Online (Sandbox Code Playgroud)
或者在Interface Builder中设置标签并检查标签.
- (IBAction)buttonPressed:(UIButton *)button {
if (button.tag == 1) {
} else if (button.tag == 2) {
}
}
Run Code Online (Sandbox Code Playgroud)
标签不是从零开始的.使用1或更大.
| 归档时间: |
|
| 查看次数: |
15376 次 |
| 最近记录: |