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

ethana

macrumors 6502a
Original poster
Jul 17, 2008
836
0
Seattle, WA
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
 

ayasin

macrumors 6502
Jun 26, 2008
318
0
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.
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
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

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.
 

ethana

macrumors 6502a
Original poster
Jul 17, 2008
836
0
Seattle, WA
Would this suffice?

Code:
MainView *myMainView;

[myMainView goTell];

Would that not call the function for the first file?
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
Would this suffice?

Code:
MainView *myMainView;

[myMainView goTell];

Would that not call the function for the first file?

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.
 

dgdosen

macrumors 68030
Dec 13, 2003
2,817
1,463
Seattle
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.

I love reading some of the questions up here.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.