1

I'm just trying to understand how delegate works and I'm in troubles.

I have two classes (both UIViewController) connected into the storyboard, the first one (ViewController.h/m) hold a TableView with cells and the second one (AddNameViewController.h/m) simply hold a TextField (where I want to write) and a button (Add Name)

as you surely understand I want the button pressed to send to the TableView what is written into the TextField, pretty simple.

And since I have two different Controllers and an Array containing the data holds by the tableview, I want to connect them with a delegate (just to learn it).

here is some code:

ViewController.h

#import "AddNameViewController.h"
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, AddNameViewControllerDelegate>
@property (strong, nonatomic) NSMutableArray *array;
@end

ViewController.m

#import "ViewController.h"
#import "AddNameViewController.h"
@inferface ViewController ()

@end

@implementation ViewController
@synthesize array;

-(void)addStringWithString:(NSString*)string
{
[self.array addObject:string];
NSLog(@"%@", array);
}

-(void)viewDidLoad
{
AddNameViewController *anvc = [[AddNameViewController alloc] init];
anvc.delegate = self;

array = [[NSMutableArray alloc] initWithObjects:@"first", @"second", nil];
NSLog(@"%@", array);
[super viewDidLoad];

}

-(NSInteger)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSindexPath*)indexPath
{
static NSString *simpleTableIdentifier = @"RecipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}

@end

AddNameViewController.h

@protocol AddNameViewControllerDelegate <NSObject>

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

@end

@interface AddNameViewController : UIViewController

@property (weak, nonatomic) id <AddNameViewControllerDelegate> delegate;
@property (weak, nonatomic) IBOutlet UITextField *myTextField;

-(IBAction)add:(id)sender;

@end

finally the AddNameViewController.m

#import "ViewController.h"

@interface AddNameViewController ()

@end

@implementation AddNameViewController
@synthesize myTextField, delegate;

-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)  {
}
return self;
}

-(void)viewDidLoad
{
[super viewDidLoad];
}

-(IBAction)add:(id)sender
{
[self.delegate addStringWithString:self.myTextField.text];
// I've also tried with this but nothing --> [self.delegate addStringWithString:@"aa"];
}

@end

The array is initialized properly, no errors, no warnings, no crashes, simply seems like the method "addStringWithString" is not even called, because is not even NSLog anything.

obviously everything in connected in the storyboard, methods and outlets, thanks for your help.

Adarkuccio
  • 973
  • 4
  • 12
  • 24

1 Answers1

0

in interface builder of AddNameViewController, did you connect the button event (Touch Up inside) into the action -(IBAction)add:(id)sender ?

also try this

-(IBAction)add:(id)sender
{
 if([self.delegate respondsToSelector:@selector(addStringWithString:)]) {
[self.delegate addStringWithString:self.myTextField.text];
}
// I've also tried with this but nothing --> [self.delegate addStringWithString:@"aa"];
}
duchuy
  • 604
  • 5
  • 9
  • Yes, the button event is connected to the "touch up inside" into the storyboard to the correct method (IBAction)add:(id)sender, obviously into the "AddNameViewController", and I've also tried with the "if statement" you mentioned but nothing happens – Adarkuccio Mar 20 '13 at 06:32
  • I've added an NSLog into the if statement you did, and nothing is printed, so the delegate is not responding to that selector, don't know why... – Adarkuccio Mar 20 '13 at 06:38
  • which viewcontroller you start first , ViewcOntroller or AddNameViewController, coz if AddNameViewController is the first, its delegate is never initialized, NSLog(self.delegate) is nil – duchuy Mar 20 '13 at 07:35
  • ViewController is the first, if i add [anvc nameMethod]; in the viewDiDLoad of the ViewController the method IS called, so the delegate IS initialized, but I want to call the method by touching the button, not at the viewDidLoad – Adarkuccio Mar 20 '13 at 11:07
  • seems that the delegate is initialized and subsequently deallocated, that's why if I add [anvc method]; into the viewdidload it works and otherwise don't, I don't know why is deallocated then.. – Adarkuccio Mar 20 '13 at 19:13
  • i'm not familiar with Storyboard, still using nib files . maybe you can try these topics: http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers or http://stackoverflow.com/questions/5244830/using-a-delegate-to-pass-data-back-up-the-navigation-stack . cheers – duchuy Mar 22 '13 at 09:26