Passing data from controllerA to controllerB using delegate

I am trying to send simple data from viewControllerA to viewControllerB using delegate but can not find The problem in my code.here is the code

in ViewControllerA.h

#import <UIKit/UIKit.h>

@class ViewControllerA;

@protocol viewControllerADelegate <NSObject>
-(void) addItem: (ViewControllerA *)controller data:(NSString *)item;
@end

@interface ViewControllerA : UIViewController
- (IBAction)buttonPressed:(id)sender;

@property (assign) id<viewControllerADelegate> delegate;
@end

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

and here is ViewControllerB.h

#import <UIKit/UIKit.h>
#import "ViewControllerA.h"

@interface ViewControllerB : UIViewController<viewControllerADelegate>

 @end

and 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.
ViewControllerA *vba = [[ViewControllerA alloc]init];
[vba 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

The implemented function -(void) addItem: (ViewControllerA *)controller data:(NSString *)item is never called.Am I missing something ? Thanks in advance

Answers


Are you using storyboards? If yes, you just push the wrong button: storyboard initializes view controllers on its own, so yours code:

ViewControllerA *vba = [[ViewControllerA alloc]init];
[vba setDelegate:self];

just setting delegate on some unused view controller. Use segues, don't try to reinvent a wheel.


Need Your Help

rails use counts in different views

ruby-on-rails ruby rails-models grails-controller

Hello i guess this is going to be pretty noob question.. But..

Git post commit: skip --amend and rebase

git githooks post-commit-hook

I have a post-commit hook that does stuff un ruby. It works very well but in some cases I would like to skip the code execution when I do a rebase or when I do a commit --amend.