View Full Version : How do I call a method from a custom class?
benkenobi
Jun 20, 2009, 02:44 AM
Hey, I need help!
I've found places online asking the same question and none of the books I bought cover this.
I have a class I made called someclass.m, someclass.h, and I am in anotherclass.m . All I'm trying to do is call method1 from someclass.m
So I try doing this:
someclass *makeClass;
makeClass = [someclass alloc];
[makeclass method1];
It doesn't work...
So I try making an IBOutlet connecting the two classes together. Then I try calling the method [madeclass method1] Still does not work. Hope someone can help me before I shoot myself. thanks.
BlackWolf
Jun 20, 2009, 04:34 AM
[[someclass alloc] init]
try that one, you always need init with alloc. also, you should really write your class names with an uppercase first character, that seperates them from variables and methods and makes it a lot easier to read!
Luke Redpath
Jun 20, 2009, 07:51 AM
It might help to read Apple's beginners guide to Objective-C before asking such fundamentally basic questions.
Saladinos
Jun 20, 2009, 08:49 AM
Read Apple's Objective-C documentation and the Cocoa Fundamentals Guide.
benkenobi
Jun 21, 2009, 02:39 AM
I tried the alloc and everything else all i get is 'classname' undeclared.
I have looked through Apples documentation but honestly it is hard to understand (I'm not familiar with the language) thats why I bought the books that are easier. If the question was so fundamentally basic I would have figured it out by now. I've done this in Java and Visual basic. Objective C seems to be a little different. I find it odd why such a simple question can't be answered whenever I look... :apple:
BlackWolf
Jun 21, 2009, 07:42 AM
I tried the alloc and everything else all i get is 'classname' undeclared.
I have looked through Apples documentation but honestly it is hard to understand (I'm not familiar with the language) thats why I bought the books that are easier. If the question was so fundamentally basic I would have figured it out by now. I've done this in Java and Visual basic. Objective C seems to be a little different. I find it odd why such a simple question can't be answered whenever I look... :apple:
it IS very simple. the code I posted above works perfectly, but of course you must make objective C aware of your custom class by importing the .h file of the class at the top of your file via #import
benkenobi
Jun 21, 2009, 11:39 AM
It would be awesome if this ever worked.
http://img194.imageshack.us/img194/8475/sn3.jpg
benkenobi
Jun 21, 2009, 11:50 AM
It's working now. You had to put -(void) methodname, in the header. The Apple documentation is useless. thanks
BlackWolf
Jun 21, 2009, 12:41 PM
It's working now. You had to put -(void) methodname, in the header. The Apple documentation is useless. thanks
the apple documentation is far from useless ^^ and yes, you have to put all your methods in your header, otherwise it will not be recognized. every objective c tutorial would have told you that ;-)
Luke Redpath
Jun 21, 2009, 12:41 PM
It's working now. You had to put -(void) methodname, in the header. The Apple documentation is useless. thanks
Actually its not useless at all. Your problem here is that you've failed to understand the basics of Objective-C - that is, you need to declare your methods in header files. You're naming conventions are also completely out of whack - classes should be CamelCased - the fact you've missed this seems to indicate you've not read the documentation or the beginner's guide to Objective-C very thoroughly and I'd suggest starting it again from the beginning.
benkenobi
Jun 21, 2009, 07:02 PM
that is, you need to declare your methods in header files. You're naming conventions are also completely out of whack - classes should be CamelCased - the fact you've missed this seems to indicate you've not read the documentation or the beginner's guide to Objective-C very thoroughly
Yea.. I've barely read more than 3 lines of it. I'm sure its not useless, but reading through the tutorial books are easier for me and a lot less boring... Unfortunately, it did not mention anything about CamelCasing or saying you HAD to declare methods in the header. I wonder why they didn't make it show up as an error like it would do with an action method.
Luke Redpath
Jun 22, 2009, 05:49 AM
It shows up as a warning because it will still compile and if the object responds to that message it will work; but because it's not declared in the header the compiler can't be sure at compile-time that it responds to that message, hence the warning.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.