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 have looked around on the Internet trying to answer this question. It seems
I've looked at every question so far and none seem to actually answer this
This question has been asked before but 1) the user never accepted an answer
Having looked at this question, I have the following code: $/ = \0 answer
I realise this question has been asked 100 times, but i've looked through the
I looked in the Rails docs under inflector and found this message... Module deprecated
I've looked through several posts on StackOverflow, but haven't been able to find an
I looked at SQL Server dateformat codes but I couldn't find dd.mm.yyyy hh:mm format
Has anyone looked at Yahoo's ASTRA ? It's fairly nifty, but I had some
So I've looked at this use of the freebase API and I was really

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.