PDA

View Full Version : Calling Class Methods




RevK
Sep 1, 2008, 04:14 PM
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!

Thanks.



robbieduncan
Sep 1, 2008, 04:22 PM
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...

whooleytoo
Sep 1, 2008, 04:24 PM
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

- (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];

RevK
Sep 1, 2008, 04:25 PM
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.

RevK
Sep 1, 2008, 04:27 PM
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.


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

robbieduncan
Sep 1, 2008, 04:28 PM
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.

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.

RevK
Sep 1, 2008, 04:33 PM
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.

whooleytoo
Sep 1, 2008, 04:45 PM
One last question: I have Obj-C 2.0 garbage collection required, so do I need to deallocate the instance of that class?

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.

kpua
Sep 1, 2008, 05:01 PM
As a matter of habit, I always release objects manually, it's more efficient than any garbage collection.

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.