简体   繁体   English

如何在两个不同的UIViewController之间使用IBAction进行委派

[英]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) 我有两个类(都UIViewController)连接到情节提要中,第一个(ViewController.h / m)持有一个带有单元格的TableView,第二个(AddNameViewController.h / m)仅持有一个TextField(我要在其中编写)和一个按钮(添加名称)

as you surely understand I want the button pressed to send to the TableView what is written into the TextField, pretty simple. 正如您确定理解的那样,我希望按下按钮将发送到TableField的内容发送到TableView,这非常简单。

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). 而且由于我有两个不同的Controllers和一个包含由tableview保存的数据的Array,所以我想将它们与一个委托连接(只是学习它)。

here is some code: 这是一些代码:

ViewController.h ViewController.h

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

ViewController.m 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 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 最后是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. 数组已正确初始化,没有错误,没有警告,没有崩溃,甚至似乎根本没有调用方法“ addStringWithString”,因为它甚至都不包含NSLog。

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 ? 在AddNameViewController的界面生成器中,您是否将按钮事件(内部Touch Up)连接到动作-(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"];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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