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 442779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:06:20+00:00 2026-05-12T21:06:20+00:00

Looked for an answer for this question, but I haven’t found a suitable one

  • 0

Looked for an answer for this question, but I haven’t found a suitable one yet. I’m hoping you guys (and gals) can help me out! (This is for an iPhone app)

Alright, I have a Mutliview application. Each view has it’s own class, and everything is happy. However, the different classes sometimes call the same method. Up until now, I have simply wrote that Method twice, in both of the class files.

This is what I want to do though:

I want to make a new class, in It’s own file, that has all the “Common” Methods. Then, whenever another class needs to call the Method, I simply call it from the other file. This way, when I want to change the Method, I only need to change it in one place, and not all the places…

I’m not sure how I’d do this, which is why I’m asking for help. I’m a little rusty and new for Objective-C, so pretty examples will help me a lot. Allow me to give you one.

File: ViewController1.m

@implementation ViewController1

//Do Some awesome stuff....

CALL "CommonMethod" HERE

@end

File: ViewController2.m

@implementation ViewController2

//Do Some awesome stuff....

CALL "CommonMethod" HERE

@end

File: CommonClass

@implementation commonClass

- (void)CommonMethod:(id)sender
{

//So some awesome generic stuff...



    }
@end

I feel like I need to #import the other file, make an Object from the class and call the Method from the Object… How do I do that?

Thanks again!

  • 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-12T21:06:20+00:00Added an answer on May 12, 2026 at 9:06 pm

    Option 1:

    @implementation commonClass
    + (void)CommonMethod:(id)sender  /* note the + sign */
    {
    //So some awesome generic stuff...
        }
    @end
    
    @implementation ViewController2
    
    - (void)do_something... {
        [commonClass CommonMethod];
    }
    
    
    @end
    

    Option 2:

    @implementation commonClass
    - (void)CommonMethod:(id)sender
    {
    //So some awesome generic stuff...
        }
    @end
    
    @implementation ViewController2
    
    - (void)do_something... {
        commonClass *c=[[commonClass alloc] init];
        [c CommonMethod];
        [c release];
    }
    
    @end
    

    Option 3: use inheritance (see Mr. Totland’s description in this thread)

    @implementation commonClass
    - (void)CommonMethod:(id)sender
    {
    //So some awesome generic stuff...
        }
    @end
    
    /* in your .h file */
    @interface ViewController2: commonClass
    
    @end
    

    naturally you always need to #import commonClass.h in your view controllers..

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

Sidebar

Related Questions

I looked around but haven't found an answer to this. I have a CentOS
I've looked for this question on stackoverflow, but didn't found a really good answer
I've looked around but haven't found an answer so I wanted to confirm this
This looked like a very simple question but I couldn't find the answer. How
I've looked everywhere for the answer to this question but cant find anything so
MySQL noob here; looked around first but couldn't find the answer to this question.
This is no doubt a newbish question, but I have looked for an answer
I have looked around on Google and StackOverflow for the answer to this question,
I've looked around and couldn't find a distinct answer to this question. So I'm
Have looked quite hard for this answer but having no luck. I have 3

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.