0

I am using a simple protocol to tell a delegate when save button was tapped on VC2 so the view controller can be dismissed by popViewControllerAnimated by VC1.

VC2 has a protocol which VC1 confirms to.

VC2.h

#import <UIKit/UIKit.h>

@class VC2;

@protocol VC2Delegate <NSObject>

- (void)saveBtnWasTpdOnVC2:(VC2 *)controller;

@end

@interface VC2 : UITableViewController

@property (weak, nonatomic) id <VC2Delegate> delegate;

- (IBAction)saveBtnTpd:(id)sender;

@end

VC2.m

- (IBAction)saveBtnTpd:(id)sender
{
    NSLog(@"save tapped");
    [self.delegate saveBtnWasTpdOnVC2:self];
}

VC1.m

- (void)saveBtnWasTpdOnVC2:(VC2 *)controller
{
    NSLog(@"saveBtnWasTpd"); // I don't see this NSLog!
    [controller.navigationController popViewControllerAnimated:YES];
}

Hope you can help.

3 Answers3

0

How did VC2 knows that VC1 is the delegate? When you pushing the view the VC2, then you must set the delegate to VC1. And VC1 must be instantiated earlier and active for the full lifetime of VC2.

karim
  • 15,408
  • 7
  • 58
  • 96
0

I think you forgot to add this line in your VC1.m

[vc2Object setDelegate:self];

Also your design is little bit confusing, because why are you sending the VC2 object back to the delegate method ?.

If you are confirming to the protocol, there should be the object of VC2. You should be setting the delegate of VC2 object to VC1 object.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
0

Make sure that you have done <VC2Delegate> in vc1.h file and assign the vc2.delegate = self;

Shah Paneri
  • 729
  • 7
  • 28