Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6084835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:34:11+00:00 2026-05-23T11:34:11+00:00

in the interest of learning how to program in Objective C I decided to

  • 0

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
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:34:12+00:00Added an answer on May 23, 2026 at 11:34 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My 12 year old brother has recently expressed an interest in learning to program.
I am currently learning C++ and I have been asked to make a program
In the interest of reusing some existing code that was defined as an instance
Out of interest of learning what's going on behind the scenes, I am running
I'm interest in learning how to use jquery comet to create real-time multi-user note
Now, I'm learning python but I'm PHP web developer. I don't interest about terminal
I have searched all over the internet for programming in Binary and the only
I am wondering what primers/guides/tutorials/etc. are out there for learning to rewrite URLs using
I recently asked a question about functional programming, and received (good!) answers that prompted
I have made an application recently which has articles, etc, with a commenting/moderation system

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.