1
votes

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?

3
Yes I put prepareForSegue in view controller 1.thisiscrazy4
Are you doing #import ViewController1 in viewController2?lnafziger

3 Answers

3
votes

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...

  1. 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.

  2. 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
    
  3. 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)

  4. 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;
    }
    
1
votes

I ran into something similar the other day. I ended up creating a delegate for vc2, and using

vc2.delegate = self;

in the segue instead. Would this solve your problem? If you need help setting up the delegate, let me know and I'll do my best to help!

0
votes

Just add a delegate to your the ViewController that you are accessing the delegate on, XCode / Obj-C seems to do the right thing afterwards.

Before:

@interface ViewController2 : UIViewController

@end

After:

@interface ViewController2 : UIViewController
  @property (nonatomic, weak) id <UIPageViewControllerDelegate> delegate;
@end