Calling Class Methods

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

  1. macrumors member

    Joined:
    Apr 26, 2004
    #1
    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:

    Code:
    
    one *theClass;
    [theClass theMethod:nil];
    
    
    Code:
    
    [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!

    Thanks.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #3
    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 :

    One.m
    Code:
    - (IBAction) theMethod: (id) sender
    {
    ...
    }
    
    Two.m :
    Code:
    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) :
    Code:
    + (IBAction) theMethod: (id) sender
    {
    ...
    }
    
    Two.m :
    Code:
    [One theMethod: nil];
    
     
  4. thread starter macrumors member

    Joined:
    Apr 26, 2004
    #4
    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. thread starter macrumors member

    Joined:
    Apr 26, 2004
    #5
    Thank you for answering so quick... I'll try that and report back. Thanks again.
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    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. thread starter macrumors member

    Joined:
    Apr 26, 2004
    #7
    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

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #8
    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

    Joined:
    Jul 25, 2006
    #9
    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