in the interest of learning how to program in Objective C I decided to make a calculator application (with 0-9, +, – buttons etc.). While I was programming I noticed that the 0-9 buttons on my application all did basically the same thing (ie: tell the only TextBox to display whatever number was pressed) so why not save time and make a function that does just that.
The problem is I don’t know where/how to declare this function so it can send a message to the TextBox. I’ve tried putting it a multitude of places and I get a multitude of errors:
MyCustomObject.h
//imports and declaring variables
@interface MyCustomObject: NSObject{
IBOutlet id Text;
/* 1. declaring function here gives the error: expected ':', ',', ';', '}' or
'__attribute__' before '{' token. It has the "{" line highlighted */
}
- (IBAction)ButtonOne:(id)sender;
//A lot of (IBAction) lines like the one above
/* 2. Declaring here gives the same error as the 1., highlighting the same line*/
@end
/* 3. Declaring here gives the error:"Text" undeclared highlighting the [text]line*/
MyCustomObject.m
//imports
/* 4. Declaring here gives the same error as 3. highlighting the same line*/
@implementation Solver
/* 5. Declaring here gives the same error as 3. highlighting the same line*/
- (IBAction)ButtonOne:(id)sender {
TheFunctionIWantToDeclare(@"1");
}
//Some more -(IBAction)'s with code
@end
Code of the function I want to declare
void TheFunctionIWantToDeclare (NSString*x)
{
[Text setStringValue:x];
//more code here but the only part thats giving me grief is the above line
}
Your idea to reduce repetition by creating a single function is excellent (this is known as the “Don’t Repeat Yourself” principle). In this case, however, the best way to do that is to create a single method, rather than a function.
One
IBActionmethod can have any number of buttons connected to it. The buttons can be distinguished by theirtag, which you can easily set in Interface Builder’s inspector. Give each of your number buttons a distinct tag; in this case it would be best to set each tag to be the number that the button represents.Then you create one single
IBActionfor all your number buttons:Note that I have changed the method and instance variable names to begin with lowercase letters; this is conventional in ObjC code. It’s also good to explicitly type your
IBOutlet(NSTextFieldrather than the genericid); this will prevent you from mistakenly connecting it to the wrong object, among other errors.Connect all the number buttons to this one action in Interface Builder. When the method is called, you simply access the button’s tag to set the text field:
As for your explicit question — where the function should be declared — you would generally put it in the .m file, above the
@implementation.* The error you’re getting is due to your trying to access an instance variable,Text, which belongs to an object, outside of any of the object’s methods. Only code inside of a method can access an object’s ivars in that way.*Although it can go just about anywhere except inside the
@interfaceblock.