我正在编写一个带有 10 个按钮的简单应用程序。一个按钮如下所示:
- (IBAction)num1pressed:(UIButton *)sender
{
// checkSomethingFirst
self.myLabel.text=[NSString stringWithFormat:@"1"];
}
每个按钮都需要“首先检查某些内容”,因此我想调用一个名为 checkSomethingFirst 的函数,而不是将代码行复制并粘贴到每个 (IBAction)num#pressed 代码段中。也许功能在这里是错误的词?不过我的功能看起来像这样:
-(void)checkSomethingFirst
{
// check first thing
// check other thing
// check last thing
}
当我编译我的代码时 - 它看起来像这样:
- (IBAction)num1pressed:(UIButton *)sender
{
checkSomethingFirst;
self.myLabel.text=[NSString stringWithFormat:@"1"];
}
我在 checkSomethingFirst 处收到错误;<--- 使用未声明的标识符“checkSomethingFirst”。
如何创建一个所有按钮都可以使用的简单函数或过程?注意我不需要返回任何值,因为我的 checkSomethingFirst 函数只是将标签设置为 0 或 1(使用 bool 可能更好,除非我在全局标签上进行设置 - 因此无需返回一个布尔值,我可以简单地询问标签中的值)。