1 Replies - 2199 Views - Last Post: 21 July 2012 - 08:43 PM

#1 nym-ph  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 27
  • Joined: 18-December 10

Objective-C - Calling a method with a delegate

Posted 21 July 2012 - 08:37 PM

Hi everyone!

I'm trying to learn iOS development, and got stuck with this. Can anyone help me please?


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.

Code is bellow.

Any help would be much appreciated!!

Thank you!


// ViewController.h

#import <UIKit/UIKit.h>

@protocol ViewControllerDelegate <NSObject>

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

@end

@interface ViewController : UIViewController <ViewControllerDelegate>;

@end



// ViewController.m

#import "ViewController.h"

@implementation ViewController

-(void)didReceiveMessage:(NSString *)string{
  NSLog(@"BANANAS");
}

@end



// 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;

@end



// 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];
}
@end


edit: just posted this in the wrong section. Should be in C/C++ Objective-C ... Sorry for the trouble!!

This post has been edited by nym-ph: 21 July 2012 - 08:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Objective-C - Calling a method with a delegate

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,287
  • Joined: 28-March 11

Re: Objective-C - Calling a method with a delegate

Posted 21 July 2012 - 08:43 PM

Move then it shall go...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1