How do I call a function from another file?

Discussion in 'iOS Programming' started by ethana, Aug 5, 2008.

  1. macrumors 6502a

    Joined:
    Jul 17, 2008
    Location:
    Seattle, WA
    #1
    In MainView.m I have one function -(void)goTell;

    I want to be able to call the function from SecondView.m. How do I do this?

    I have declared the function in the MainView.h and added #include MainView.h to SecondView.m, but it still doesn't work.

    Ethan
     
  2. macrumors 6502

    Joined:
    Jun 26, 2008
    #2
    Your description is insufficent to diagnose the issue. Can you post the code? Also I suggest using #import rather than #include as it will protect against multiple inclusion.
     
  3. macrumors 68040

    Joined:
    Apr 22, 2005
    #3
    I assume MainView.m contains the implementation of a class called MainView, and likewise SecondView.m has the implementation of a class called SecondView. In order to call a function that is part of the MainView class, you either need an instance of the class to call it on (technically you need an instance to send a message to) or if it is a static function, you can send the method directly to the class name. In your case the function is declared with a - so you're going to need an instance to send the message to.

    If you don't understand what I am saying here, you should brush up on Objective-C and object oriented programming.
     
  4. thread starter macrumors 6502a

    Joined:
    Jul 17, 2008
    Location:
    Seattle, WA
    #4
    Would this suffice?

    Code:
    MainView *myMainView;
    
    [myMainView goTell];
    
    Would that not call the function for the first file?
     
  5. macrumors 68040

    Joined:
    Apr 22, 2005
    #5
    Not exactly. First of all, myMainView is nothing, because you haven't allocated any memory for it or initialized it. There is probably another issue depending on what exactly goTell does. Are you actually trying to tell something to the real MainView that you presumably display before going to secondview? Then you need to send the message to the same instance that is actually displayed by your program, not a new instance that you create just to send the message to.

    Again, if this doesn't make sense, I would strongly recommend reading up on Objective-C. What you are asking about is one of the very fundamental issues in object-oriented programming that you should really have a good grasp of before you attempt anything as complex as writing an iPhone app.
     
  6. macrumors 65816

    dgdosen

    Joined:
    Dec 13, 2003
    Location:
    Seattle
    #6
    I love reading some of the questions up here.
     

Share This Page