views:

47

answers:

2

So I have a ViewController Class which is my first view, I then add a subview to it, pseudo code code below;

ViewController2 *viewController2 = [[ViewController2 alloc] init]; 
[self addSubview:viewController2.view];

My question is, in ViewController1, I have a method that does an animation and removes the view. How can I call that method from a button on ViewController 2?

I tried doing something like in ViewController2...

ViewController1 *viewController1 = [[ViewController1 alloc] init]; 
[viewController1 back]; //Trying to call method in other class from ViewController2

And its funny because in the "back" method on ViewController1,I have an NSLog statement, and it works, but none of the other code works. So I know the method is getting called from my other class, but only the NSLog part of it executes? Any Suggestions.

SO basically I want to call a method of ViewController1 from ViewController2

A: 

You can't create a second instance of ViewController1 and get the effect you're looking for. You need to call it on the instance that has already been created.

In this case, you can accomplish it by creating an ivar in ViewController2 to remember who the parent is and assign it when you add in the other VC. Let's say you call the ivar owningViewController1. Add a property for it in ViewController2.h and synthesize it at the top of ViewControler2.m, then change your example to:

ViewController2 *viewController2 = [[ViewController2 alloc] init]; 
[self addSubview:viewController2.view];
[viewController2 setOwningViewController1:self];

Then, don't bother creating a new ViewController1 instance, just use the one saved:

[owningViewController1 back];
John Franklin
Ahhh that did the trick! Thank you very much John, I really appreciate it.
Nicholas L. Eby
The solution John gave me worked perfectly and did the trick. My latter question now is - is there anyway to get rid of the warning "'UIViewController' may not respond to '-back'"? The method works works, just get the warning because its defined else where.
Nicholas L. Eby
Sounds like the ivar is defined as a UIViewController, not as a ViewController1. In the ViewController2 header you can add "@class ViewController1;" to forward define the VC2 class, then in ViewController2 make sure you #import "ViewController1.h" so the compiler knows about all the VC1 methods.
John Franklin
A: 

The solution John gave me worked perfectly and did the trick. My latter question now is - is there anyway to get rid of the warning "'UIViewController' may not respond to '-back'"? The method works works, just get the warning because its defined else where.

Nicholas L. Eby
@Nicholas. Welcome to Stack Overflow. If you liked John's answer, you should click the "checkmark" icon to accept his answer, and maybe throw him a vote up as well. To ask further questions, either comment on his answer, or edit your question. It's not really a forum so leaving another "answer" doesn't really do what you mean.
quixoto
Quixoto - Thanks, I'm new at this whole stackoverflow thing.
Nicholas L. Eby