1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Calling Class Methods

Discussion in 'Mac Programming' started by RevK, Sep 1, 2008.

  1. macrumors member

    Hello MacRumors,

    Here's my deal: I have Class "one" and Class "two". Class one has an IBAction which does soemthing, and I'd like to be able to call that method from class two. I've tried these things:

    one *theClass;
    [theClass theMethod:nil];
    [theClass theMethod:nil];
    It either crashes or doesn't work at all. I have also imported class one's header file at the top of the implementation of class two. Help would be much appreciated!

  2. Moderator


    Staff Member

    It would be extremely unusual for an IBAction to be a class method. Almost always they are instance methods. In which case you need to call the method on an instance of the class not the class itself.

    It really sounds like you haven't got a handle on Object-Oriented programming and should probably do some background reading...
  3. macrumors 603


    I'm not really sure what you want to do. Since you're talking about an IBAction, I'm guessing you don't mean a class method, but an instance method.

    I'll try and give the answer either way:

    If you're calling an instance method from another class, then you need to create an instance of that class (an object) before you can call the method. You weren't doing this :

    - (IBAction) theMethod: (id) sender
    Two.m :
    One* myOne = [[One alloc] init];
    [myOne theMethod: nil];
    If you are actually trying to call a class method from another class:

    One.m (not the "+" which denotes a class method) :
    + (IBAction) theMethod: (id) sender
    Two.m :
    [One theMethod: nil];
  4. macrumors member

    Oops, I mean't it is an instance method...:rolleyes:

    And yes, I'm a noob with Objective-C. I've done background reading, which is why I am posting here, because I didn't find the answer to my question.
  5. macrumors member

    Thank you for answering so quick... I'll try that and report back. Thanks again.
  6. Moderator


    Staff Member

    Well you need an instance of the class to call the method on. The thing is if you just create in instance as suggested above it'll almost certainly not do what you want: you are almost want to call the method on an existing object that is linked to your UI (thus the IBAction). You really need to tell us a lot more about the structure of the application.
  7. macrumors member

    Thanks whooleytoo, it works.

    Sorry robbieduncan, I mean't to call it an instance method from the start.

    One last question: I have Obj-C 2.0 garbage collection required, so do I need to deallocate the instance of that class?

    Thanks for all of your help... this is truly one great place.
  8. macrumors 603


    If you're developing on the Mac, for 10.5 then you don't need to deallocate.

    If you're developing for anything else, you do.

    As a matter of habit, I always release objects manually, it's more efficient than any garbage collection.
  9. macrumors 6502

    Although, under GC, retain/release/autorelease are essentially no-ops. If he's compiling GC-required and isn't planning on switching back to non-GC, then there's really no point to cluttering the code with those method calls.

Share This Page