How do I call a method from a custom class?

Discussion in 'iOS Programming' started by benkenobi, Jun 19, 2009.

  1. benkenobi macrumors member

    Joined:
    May 27, 2009
    #1
    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:
    Code:
    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.
     
  2. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #2
    Code:
    [[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!
     
  3. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #3
    It might help to read Apple's beginners guide to Objective-C before asking such fundamentally basic questions.
     
  4. Saladinos macrumors 68000

    Saladinos

    Joined:
    Feb 26, 2008
    #4
    Read Apple's Objective-C documentation and the Cocoa Fundamentals Guide.
     
  5. benkenobi thread starter macrumors member

    Joined:
    May 27, 2009
    #5
    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:
     
  6. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #6
    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
     
  7. benkenobi thread starter macrumors member

    Joined:
    May 27, 2009
  8. benkenobi thread starter macrumors member

    Joined:
    May 27, 2009
    #8
    It's working now. You had to put -(void) methodname, in the header. The Apple documentation is useless. thanks
     
  9. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #9
    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 ;-)
     
  10. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #10
    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.
     
  11. benkenobi thread starter macrumors member

    Joined:
    May 27, 2009
    #11
    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.
     
  12. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #12
    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.
     

Share This Page