I am trying to send a simple string from One viewcontroller to another viewcontroller using delegate but my delegate is not working. I am using storyboard and dont want to use segue to pass data. Here is my simple code
ViewControllerA.h
#import <UIKit/UIKit.h>
@class ViewControllerA;
@protocol viewControllerADelegate <NSObject>
-(void) addItem: (ViewControllerA *)controller data:(NSString *)item;
@end
@interface ViewControllerA : UIViewController{
__weak id <viewControllerADelegate> delegate;
}
- (IBAction)buttonPressed:(id)sender;
@property (weak) id<viewControllerADelegate> delegate;
@end
ViewControllerA.m
#import "ViewControllerA.h"
@interface ViewControllerA ()
@end
@implementation ViewControllerA
@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)buttonPressed:(id)sender {
[self.delegate addItem:self data:@"this is data"];
}
@end
ViewControllerB.h
#import <UIKit/UIKit.h>
#import "ViewControllerA.h"
@interface ViewControllerB : UIViewController<viewControllerADelegate>
@end
ViewControllerB.m
#import "ViewControllerB.h"
#import "ViewControllerA.h"
@interface ViewControllerB ()
@end
@implementation ViewControllerB
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewControllerA *vca = [storyboard instantiateViewControllerWithIdentifier:@"A"];
[vca setDelegate:self];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) addItem: (ViewControllerA *)controller data:(NSString *)item{
NSLog(@"delegate function called");
}
@end
Am I missing something silly ? The delegate method -(void) addItem: (ViewControllerA *)controller data:(NSString *)item
is not triggered.