Reputation: 3
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
}
Upvotes: 0
Views: 590
Reputation: 1208
as suggested by Josh Caswell, you can do it by placing as many buttons needed on the main view or any view and just assign the tag values in the IBOutlet and the tag value differentiates the buttons in your case. in your case you need almost 15-18 buttons i guess.using IB will reduce your work
TNQ
Upvotes: 0
Reputation: 64002
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 IBAction
method can have any number of buttons connected to it. The buttons can be distinguished by their tag
, 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 IBAction
for all your number buttons:
@interface MyCustomObject: NSObject{
IBOutlet NSTextField * myTextField;
}
- (IBAction) numberButtonPressed: (id)sender;
// Other methods...
@end
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
(NSTextField
rather than the generic id
); 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:
- (IBAction) numberButtonPressed: (id)sender {
[myTextField setIntegerValue:[sender tag]];
}
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 @interface
block.
Upvotes: 1
Reputation: 417
first think you need to understand is each UIObject has tag value. for example if you declare a button means by default it has property named tag value. for the number 1 you set 1 means using this tag you can access the value.
{
nslog(@"%d",sender.tag);
TheFunctionIWantToDeclare(sender.tag);
}
Upvotes: 0