I have 2 view controllers, call them ViewController1 and ViewController2. A modal segue is invoked from ViewController1 when I want to load ViewController2. I have a method in ViewController1 that needs to be called at some point when ViewController2 is showing. My idea is to have a property in ViewController2 that is a reference to ViewController1 so that I can get access to the method.
@property (strong, nonatomic) ViewController1 *vc1Reference;
This property would be set in the prepareForSegue method like so:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // 1
if ([[segue identifier] isEqualToString:@"sequeToVC2"]) { // 2
ViewController2 *vc2 = segue.destinationViewController; // 3
vc2.vc1Reference = (ViewController1*)segue.sourceViewController; // 4
}
}
However line 4 gives me this error: Implicit conversion of an Objective-C pointer to ‘int *’ is disallowed with ARC.
How am I supposed to set the reference?
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:And you set the delegate in your
prepareForSegue:(‘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
@interfaceThen 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
@interfaceline in vc1 add the protocol name in angle brackets:This arrangement allows vc2 to pass a method to vc1 without having to
#includevc1 or know anything else about it.more detail…
This is the correct form of your
Note the use of
weakreference. You don’t want to make astrongreference 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 astrongreference 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:
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:
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
(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: