-2

I've been searching high and low for a solution, and those that I've found and what I expected to work, just haven't worked for me...

For example, say I've got viewController1 and viewController2.

in viewController1, say I've got a method like below:

-(void)method1
{
   NSLog(@"Method1 called");
}

in viewController2.m I would expect to import viewController1.h and do the following:

-(void)method2
{
   viewController1 *view = [[viewController1 alloc] init];
   [view method1];
}

Is it not this simple? I've seen many threads giving this sort of answer, but it just won't work for me.

Thanks in advance.

Tim
  • 8,932
  • 4
  • 43
  • 64
Nick Farrant
  • 207
  • 1
  • 3
  • 10
  • that's the expected pattern, yes, so there must be something else going on. Can you paste some fuller code? – Cocoadelica Mar 18 '14 at 17:59
  • 5
    I have a son, Freddy. I want Freddy to clean his room. So I have a baby, Jimmy. I wait until Jimmy is old enough to speak. I tell Jimmy “Clean your room!” Then I put Jimmy up for adoption. Why isn't Freddy's room clean after all this? – rob mayoff Mar 18 '14 at 18:00
  • You getting any errors? Did you add a breakpoint and step through the code? What happens when you step through the code? – Jeremy Mar 18 '14 at 18:01
  • its very low quality question ? why dont you post code you have tried so far? – Pawan Rai Mar 18 '14 at 18:03
  • 2
    I guess that's not your real code? Your problem is most likely that you create a new instance of `ViewController1`, but you actually want to use the instance you already have (wherever it comes from). See if you can get something useful out of this question and its answers: [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Matthias Bauch Mar 18 '14 at 18:14
  • @robmayoff please specify if Jimmy and Freddy shared a room. Not enough info here. – Nick Farrant Mar 18 '14 at 18:14
  • In what aspect is it not working? The compiler can't find the method? The method isn't being called? Does the .h interface for ViewController1 contain the definition for method1? We need more code/info. – Tim Mar 18 '14 at 18:15
  • no its not real code, but I haven't got any to hand at the moment unfortunately, but that's the way I've been doing it. thanks I'll have a look! – Nick Farrant Mar 18 '14 at 18:17
  • @Jeff I can reference variables, but not methods. If that helps. thanks – Nick Farrant Mar 18 '14 at 18:17
  • Does the .h file for viewController1 define the method (in your example, method1) you wish to call from viewController2? – Tim Mar 18 '14 at 18:19
  • Perhaps you didn't understand my metaphor. You are creating an entirely new instance of `viewController1` and sending it a message. You probably want to send the message to some existing instance of `viewController1`. This means you need to learn what classes and instances are (since your question and code indicate that your understanding of them is muddled) and figure out how to pass a reference to the **existing** instance of `viewController1` to your instance of `viewController2`. – rob mayoff Mar 18 '14 at 19:25
  • @Jeff thanks - defined the methods in my .h and im all sorted. – Nick Farrant Mar 18 '14 at 20:02
  • @NickFarrant Glad it helped. Please can you mark my answer as correct. – Tim Mar 18 '14 at 20:49

1 Answers1

1

You will need to define the methods in the header file of a UIViewController for the compiler to be able to see them. (At run-time, this is another story).

Using your example:

ViewController1.h

@interface ViewController1 : UIViewController

- (void)method1;

@end

ViewController1.m

@implementation

- (void)method1 {
    NSLog(@"Method1 called");
}

@end

Then in your second UIViewController, the compiler will be able to see the method.

Tim
  • 8,932
  • 4
  • 43
  • 64