Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RevK

macrumors member
Original poster
Apr 26, 2004
65
0
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
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];
 

RevK

macrumors member
Original poster
Apr 26, 2004
65
0
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

macrumors member
Original poster
Apr 26, 2004
65
0
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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

macrumors member
Original poster
Apr 26, 2004
65
0
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

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
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

macrumors 6502
Jul 25, 2006
294
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.