Note that there are some explanatory texts on larger screens.

plurals
  1. POCall methods between MainViewController.m and FlipsideViewController.m
    text
    copied!<p><strong>Please see below edit for current (minor) issue</strong></p> <hr> <p><em>First of all, I'm new to this Obj C stuff, coming from javascript/PHP, I'm having a hard time dealing with the syntax so please bare with me here.</em> </p> <p>For hours now, I've been trying to <strong>call methods</strong> (methods right, not functions?) <strong>"between" the MainViewController.m and the FlipsideViewController.m</strong> -- from one file/class to another. </p> <p>I guess this is what's often referred to as "<strong>Call methods from another class</strong>". There are plenty of such questions around, I know, but I just can't get it to work properly.</p> <p>In my case, I have <strong>several user defined methods/functions in both above mentioned files</strong>. Sometimes, <strong>I need to call a method from within the FlipsideViewController.m that lies within the MainViewController.m File</strong>:</p> <pre><code>// in MainViewController.m - (void) calculateDays { //executes caluculations // inserts data into labels, etc } </code></pre> <p>If I want to call this function <strong>simply from within the same file</strong>, I just do:</p> <pre><code>[self calculateDays]; </code></pre> <p>That's easy, however, I want to call this function from within the FlipsideViewController.m file too, as well as vice versa. <strong>So how do I do this</strong>? <a href="https://stackoverflow.com/questions/3572448/objective-c-call-function-on-another-class">This</a>, <a href="https://stackoverflow.com/questions/7290156/call-a-class-method-from-within-that-class">this</a> and <a href="https://stackoverflow.com/questions/6830749/objective-c-calling-method-on-another-class">this</a> questions sort of answer it but it doesn't quite work to me. I'll explain why in just a second.</p> <p><strong>This is what I've tried and think should work:</strong></p> <pre><code>MainViewController *mvs = [[MainViewController alloc] init]; //alloc init MVC [mvs calculateDays]; //call "external" function </code></pre> <p>It gives me the error: <em>"Unknown type name MainViewController"</em>. So I assume I <strong>have to include/import</strong> it somehow for it to work (just like in javascript or PHP). So I include it in the FlipSideViewController.m class: </p> <pre><code> #import "MainViewController.h" </code></pre> <p>Great <em>no errors</em> so far. Then I try to <strong>compile/build</strong> it and runs into another error: <em>"clang: error: linker command failed with exit code 1 (use -v to see invocation)" "ld: 3 duplicate symbols for architecture armv7s"</em></p> <p>This <em>leads me to think</em> that <strong>importing the MainViewController like that isn't the way to go</strong> as I then import lots of other stuff that may interfere with some code in the FlipSideViewController class.</p> <p>I've <em>tried similar solutions</em> 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.</p> <hr> <h2>Edit!</h2> <p>The <strong>proposed solution by H2CO3 did solve most of the issues</strong> (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: <strong>change the content of a UILabel (UIOutlet)</strong>. Please see if anyone of you can help me with this:</p> <p>When the method is called from within self (i.e. [self calculateDay]), the value is successfully inserted into the UILabel. When called from <strong>FlipsideViewController</strong>, the value to be inserted exists and is processed successfully, but can't be inserted into the UILabel. Please se below. </p> <p><strong>Some loggings:</strong></p> <pre><code>//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 */ </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload