简体   繁体   中英

how to delegate with an IBAction between two different UIViewController

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.

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"];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM