I’m writing a simple app with 10 buttons on it. One button looks like this:
- (IBAction)num1pressed:(UIButton *)sender
{
// checkSomethingFirst
self.myLabel.text=[NSString stringWithFormat:@"1"];
}
Every button needs to “check something first” so rather than copy and paste the lines of code into every (IBAction)num#pressed code segment, I want to call a function called checkSomethingFirst. Maybe function is the wrong word here? Nevertheless my function looks like this:
-(void)checkSomethingFirst
{
// check first thing
// check other thing
// check last thing
}
When I compile my code – which looks like this:
- (IBAction)num1pressed:(UIButton *)sender
{
checkSomethingFirst;
self.myLabel.text=[NSString stringWithFormat:@"1"];
}
I get an error at checkSomethingFirst; <— Use of undeclared identifier ‘checkSomethingFirst’.
How do I create a simple function or procedure that all of my buttons can use? Note I don’t need to return any values as my checkSomethingFirst function simply sets a label to either a 0 or a 1 (probably better off using a bool, except I’m doing the setting on a global label – hence no need to return a bool, I can simply interrogate the value in the label).
You need to say what object the method should be called on
[self checkSomethingFirst];