Search code examples
iphoneprotocolsdelegationpopviewcontrolleranimated

Delegation not working properly (should tell a VC to popViewControllerAnimated)


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.


Solution

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