Lindros
Lindros

Reputation: 253

Call methods between MainViewController.m and FlipsideViewController.m

Please see below edit for current (minor) issue


I'm trying to call methods (methods right, not functions?) "between" the MainViewController.m and the FlipsideViewController.m -- from one file/class to another.

I guess this is what's often referred to as "Call methods from another class". There are plenty of such questions around, I know, but I just can't get it to work properly.

In my case, I have several user defined methods/functions in both above mentioned files. Sometimes, I need to call a method from within the FlipsideViewController.m that lies within the MainViewController.m File:

// in MainViewController.m

- (void) calculateDays {
    //executes caluculations
   // inserts data into labels, etc 
}

If I want to call this function simply from within the same file, I just do:

[self calculateDays];

That's easy, however, I want to call this function from within the FlipsideViewController.m file too, as well as vice versa. So how do I do this? This, this and this questions sort of answer it but it doesn't quite work to me. I'll explain why in just a second.

This is what I've tried and think should work:

MainViewController *mvs = [[MainViewController alloc] init]; //alloc init MVC
[mvs calculateDays]; //call "external" function

It gives me the error: "Unknown type name MainViewController". So I assume I have to include/import it somehow for it to work (just like in javascript or PHP). So I include it in the FlipSideViewController.m class:

 #import "MainViewController.h"

Great no errors so far. Then I try to compile/build it and runs into another error: "clang: error: linker command failed with exit code 1 (use -v to see invocation)" "ld: 3 duplicate symbols for architecture armv7s"

This leads me to think that importing the MainViewController like that isn't the way to go as I then import lots of other stuff that may interfere with some code in the FlipSideViewController class.

I've tried similar solutions but nothing seems to work. Can anyone please explain to me what I'm doing wrong, and perhaps how to do this properly: Call methods between MainViewController.m and FlipsideViewController.m and vice versa.


The proposed solution by H2CO3 did solve most of the issues (XCode bugged for a while and give me random errors which forced me to rebuild the entire project) but there's still this one thing that doesn't quite work: change the content of a UILabel (UIOutlet). Please see if anyone of you can help me with this:

When the method is called from within self (i.e. [self calculateDay]), the value is successfully inserted into the UILabel. When called from FlipsideViewController, the value to be inserted exists and is processed successfully, but can't be inserted into the UILabel. Please se below.

Some loggings:

//method called from within self on viewDidLoad: [self calculateDay];
Processed value to update label with: 26
New value in outlet after having been inserted: 26


//method called from another (FlipsideViewController) class file: [mvs calculateDay];
Processed value to update label with: 26
New value in outlet after having been inserted: (null)

/* 
  This doesn't work either from that external file: 
  [[mvs LabelName] setText:@"Hello, update label!"]; no errors but no display either

*/

Upvotes: 0

Views: 535

Answers (2)

Justin Meiners
Justin Meiners

Reputation: 11113

(layman's terms) In Objective-C you can only use objects that each file knows about. In this example you are trying to use a MainViewController in the FlipsideController.m file. The FlipsideController.m has no idea what a MainViewController is, so it throws errors because it doesn't know what it is or how to use it. You have two options for telling the Flipsidecontroller what a MainViewController is, you can import the header (#import "MainViewController.h") which will give you full access to everything defined in the FlipSideController.h. (You should probably never import a .m unless you really know what your doing) You can also create a forward declaration - @class FilpsideControllerin the .h and import the file in the .m. This is useful to avoid circular imports etc.

Upvotes: 0

user529758
user529758

Reputation:

If you import the header instead, that should give you all the necessary declarations, but you won't have "duplicate symbol" linker errors. This is a "standard"/common practice for writing (Objective-)C code.

#import "MainViewController.h"
                            ^
      ".h" instead of ".m" -+

Upvotes: 2

Related Questions