Take the 2-minute tour ×

I tried to replicade a couple of posts here on stackoverflow, for passing data and make calls with 2 view controllers, but without success.

So I have my ViewController (1st) and EditableViewController (2nd)

When I'm in my EditableViewController (2nd), I click a Button that triggers an IBAction with the method SendTextToViewController. After that, I was expecting for the method didReceiveMessage, in my ViewController (1st) to run, and show me "BANANAS" with NSLog.

Ultimately, I want to send data from my second Controller, to the First Controller.

I'm having a hard time understanding this delegates thing. And I have read a dozen tutorials, videos e documentation trying to figure this out. I think I'm almost there, but can't seem to figure why this is not working.

Can anyone help me please? The code is bellow.

Thank you!

// ViewController.h

#import <UIKit/UIKit.h>

@protocol ViewControllerDelegate <NSObject>

-(void)didReceiveMessage:(NSString *)string;


@interface ViewController : UIViewController <ViewControllerDelegate>;


// ViewController.m

#import "ViewController.h"

@implementation ViewController

-(void)didReceiveMessage:(NSString *)string{


// EditableViewController.h

#import <UIKit/UIKit.h>

@protocol ViewControllerDelegate;

@interface EditableViewController : UIViewController

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

@property (nonatomic, weak) IBOutlet UITextField *TextField;
@property (nonatomic, weak) IBOutlet UIButton *SendTextToViewController;

- (IBAction)SendTextToViewController:(id)sender;


// EditableViewController.m

#import "EditableViewController.h"
#import "ViewController.h"

@implementation EditableViewController

@synthesize delegate;
@synthesize TextField;
@synthesize SendTextToViewController;

- (IBAction)SendTextToViewController:(id)sender {
               [delegate didReceiveMessage:TextField.text];

- (void)viewDidUnload {
          [self setTextField:nil];
          [super viewDidUnload];
share|improve this question
Try loggging delegate in (IBAction)SendTextToViewController:(id)sender to check if your delegate is still there, it might get deallocated –  Vatev Jul 22 '12 at 14:48
You don't show anything in your code that sets the delegate variable to point to your view controller. Do you do that? –  Phillip Mills Jul 22 '12 at 14:53
See Robert Vojta's answer. He's right, you did the opposite from what you should. Long story short, what you should do is populate your EditableViewController from your ViewController (1st) (navigation for example), and make your ViewController (1st) a delegate of your EditableViewController. And then when you click a button in EditableViewController, it sends a message to ViewController (1st) for the proper method call. I can provide more details if you need. –  Raymond Wang Jul 23 '12 at 13:43
Fixed! Thank you for everyones help. I was indeeed doing it the wrong way. And Phillip was right also. Thank you! –  nmdias Jul 23 '12 at 19:39
add comment

1 Answer

up vote 3 down vote accepted

You did it in opposite way ... Do this ...

  • create EditableViewControllerDelegate,
  • your ViewController should conform to EditableViewControllerDelegate protocol,
  • assign your ViewController instance as a delegate of EditableViewController instance
share|improve this answer
This is the correct answer. In addition, make sure for step two that you conform to the protocol in the @interface line like this <EditableViewControllerDelegate> or whatever name you assign it. You are conforming incorrectly in your post above. –  Matt Meyers Jul 22 '12 at 16:36
Thank you very much for the help! That finally worked. I think I'm sarting to get how it works now =) –  nmdias Jul 23 '12 at 19:36
add comment

Your Answer


