You are on the right track, but the correct way to do this is to use a delegate.
You declare a delegate property in your vc2 @interface
:
@property (nonatomic, weak) id <vc2delegate> delegate //[1] (in vc2.h)
And you set the delegate in your prepareForSegue
:
vc2.delegate = self; //[2] (in vc1.m)
('self' is the correct reference for vc1, from vc1)
In vc2 you define a protocol, which is the method that you expect vc1 to respond to from vc2. Put this in vc2.h, above your @interface
@protocol vc2delegate //[3] (in vc2.h)
- (void) delegateMethod;
@end
Then you have to ensure you implement that method in vc1. Also you need to let vc1 know to conform to the delegate. Import vc2 into vc1.h, and on your @interface
line in vc1 add the protocol name in angle brackets:
#import vc2.h
@interface vc1 <vc2delegate> //[4] (in vc1.h)
This arrangement allows vc2 to pass a method to vc1 without having to #include
vc1 or know anything else about it.
more detail...
This is the correct form of your
@property (strong, nonatomic) ViewController1 *vc1Reference;
Note the use of weak
reference. You don't want to make a strong
reference as you don't really want to have anything to do with the delegate except to know it can handle methods you specify in your protocol. The delegate is often the object that created the delegator, creating a strong
reference back in the other direction can cause memory leaks as neither object can go out of existence.
this is the correct form of your line:
vc2.vc1Reference = (ViewController1*)segue.sourceViewController;
Note that we are NOT using type/casting in 1 or 2. For maximum code reuse/decoupling we dont want to make any suppositions on the type of object at either end of the segue.
I am assuming that your 'prepareForSegue' is in vc1. If it is not then the line would look like this:
vc2.delegate = segue.sourceViewController
This is the protocol declaration. It goes in the header file for vc2. vc2 is publishing it's expectations of any object that chooses to become its delegate. vc2 will be sending messages according to this protocol so any delegate needs to respond in the correct way. You can guard against failure in vc2 by using this kind of message-passing
if (self.delegate respondsToSelector:@selector('delegateMethod')
{
[self.delegate delegateMethod];
}
(that is an example of the kind of message passing you would use in vc2 to communicate back to vc1. you can obviously pass paremeters and get returned results back if need be)
this is a helper for the compiler which can issue you with warnings if you fail to implement the protocol.
Finally somewhere in your object definition you need to implement the method:
- (void) delegateMethod
{
// someaction;
}
#import ViewController1
inviewController2
? – lnafziger