如何使用委托从一个视图控制器传递消息到另一个使用委托?

主要故事板图像我有一个名为“Contacttableviewcontroller”的表视图控制器和一个视图控制器为“Detailviewcontroller”。 我有2个文本字段在我的视图控制器给联系人姓名和号码。 我有一个button来保存。 当我点击那个button时,它应该显示在Table View Controller中。 我正在使用的概念是使用委托从目的地到源代码传递信息。 这里是我的代码不正常工作。

Detailviewcontroller.h @protocol detailviewcontrollerdelegate<NSObject> - (void)additem:(NSString *)Name MOBILE:(NSString *)Mobile; @end @interface DetaiViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *nametextfield; @property (weak, nonatomic) IBOutlet UITextField *mobiletextfield; - (IBAction)Save:(id)sender; @property(nonatomic,weak)id<detailviewcontrollerdelegate>delegate; Detailviewcontroller.m - (IBAction)Save:(id)sender { [self.delegate additem:self.nametextfield.text MOBILE:self.mobiletextfield.text]; } Contacttableviewcontroller.h @interface ContactTableViewController : UITableViewController<detailviewcontrollerdelegate> @property(strong,nonatomic)NSString *contactname; @property(strong,nonatomic)NSString *contactno; -(void)reloadtabledata; @property(strong,nonatomic)NSArray *contactnamearray; @property(strong,nonatomic)NSArray *contactnoarray; @end Contacttableviewcontroller.m - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 4; } - (void)additem:(NSString *)Name MOBILE:(NSString *)Mobile { self.contactname=Name; self.contactno=Mobile; self.contactnamearray=@[self.contactname]; self.contactnoarray=@[self.contactno]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellreuse" forIndexPath:indexPath]; cell.textLabel.text=[_contactnamearray objectAtIndex:indexPath.row]; cell.detailTextLabel.text=[_contactnoarray objectAtIndex:indexPath.row]; return cell; } -(void)reloadtabledata { [self.tableView reloadData]; } 

首先,你需要检查,如果你已经附上你的行动方法, -Save:button或不button。 您可以通过故事板或以编程方式附加它。 要通过故事板来做到这一点,给你的button像saveButton(不强制)的名称,然后像往常一样通过ctrl拖动来附加它。

确保你正确地连接了所有的IBOutlets。

在这里输入图像说明

在这里输入图像说明

PS:我已经用适当的命名约定更新了variables的名字。 在命名variables的同时,您还应遵循骆驼案例惯例。

这里是DetailViewController.h代码 –

 #import <UIKit/UIKit.h> @protocol DetailViewControllerDelegate; @interface DetailViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *nameTextField; @property (weak, nonatomic) IBOutlet UITextField *mobileTextField; @property(strong, nonatomic) IBOutlet UIButton *savebutton; - (IBAction)Save:(id)sender; @property(nonatomic,weak)id<DetailViewControllerDelegate>delegate; @end @protocol DetailViewControllerDelegate<NSObject> - (void)additem:(NSString *)Name MOBILE:(NSString *)Mobile; @end 

DetailViewController.m

  #import "DetailViewController.h" @interface DetailViewController () @end @implementation DetailViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (IBAction)Save:(id)sender { [self.delegate additem:self.nameTextField.text MOBILE:self.mobileTextField.text]; [self.navigationController popToRootViewControllerAnimated:YES]; } @end 

现在,如果你在行动方法中join了一个断点,你会看到,它正在被调用。 你可以看到额外的一行代码,
[self.navigationController popToRootViewControllerAnimated:YES]; – 这是确保当你按下保存button,它不仅发送数据,而且还返回给你的TableView控制器来显示你的结果。

现在,您需要确保您的DetailViewController知道谁将实现其委托。 所以,在您的ContactTableViewController中,无论您在何处初始化您的DetailViewController,都必须将其委派分配给self。

所以,经过一些调整, ContactTableViewController.h类看起来像 –

 #import <UIKit/UIKit.h> #import "DetailViewController.h" @interface ContactTableViewController : UITableViewController<DetailViewControllerDelegate> @property(strong,nonatomic)NSString *contactName; @property(strong,nonatomic)NSString *contactNo; -(void)reloadtabledata; @property(strong,nonatomic)NSMutableArray *contactNameArray; //need to be mutable array, so that the data can keep appending @property(strong,nonatomic)NSMutableArray *contactMobileNoArray; //same as above @end 

现在,文件中有一些小的修改,但是评论应该澄清目的。

所以, ContactTableViewController.m文件看起来像

 #import "ContactTableViewController.h" @interface ContactTableViewController () @end @implementation ContactTableViewController - (void)viewDidLoad { [super viewDidLoad]; //Make sure you initialize the array before tryig to add any element self.contactNameArray =[[NSMutableArray alloc]init]; self.contactMobileNoArray=[[NSMutableArray alloc]init]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark- #pragma mark- TableView Datasource methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.contactNameArray count]; //you need to set the row count as the same as the array elements } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellreuse" forIndexPath:indexPath]; cell.textLabel.text=[self.contactNameArray objectAtIndex:indexPath.row]; cell.detailTextLabel.text=[self.contactMobileNoArray objectAtIndex:indexPath.row]; return cell; } -(void)reloadtabledata { [self.tableView reloadData]; } #pragma mark- Segue method -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ [segue.destinationViewController setTitle:@"Add Details"]; DetailViewController *vc = [segue destinationViewController]; vc.delegate=self; // By this, you just told your TableViewController that it is responsible for the implementation of the DetailViewController's delegate } #pragma mark- DetailViewController's Delegate method - (void)additem:(NSString *)Name MOBILE:(NSString *)Mobile { self.contactName=Name; self.contactNo=Mobile; [self.contactNameArray addObject:self.contactName]; //added the new entry [self.contactMobileNoArray addObject:self.contactNo]; //added the new entry [self.tableView reloadData]; //reload the table right after you updated the arrays } @end 

这应该可以帮助你解决所有的问题。 如果ContactTableViewController.m文件中有更改,我添加了一个或多个注释。 我试图运行该应用程序,这是正常工作。

嘿只要在您的视图添加此行的负载方法的Contacttableviewcontroller。

  DetailViewController *detailVc=[DetailViewController alloc]init]; detailVc.delegate =self; 

试试这个

detailviewcontrollerdelegate

 #import <Foundation/Foundation.h> @protocol detailviewcontrollerdelegate<NSObject> - (void)additem:(NSString *)Name MOBILE:(NSString *)Mobile; @end 

Detailviewcontroller.h

 #import <UIKit/UIKit.h> #import "detailviewcontrollerdelegate.h" @interface DetaiViewController : UIViewController{ id< detailviewcontrollerdelegate > delegate; } @property (nonatomic, assign) id< detailviewcontrollerdelegate > delegate; @property (weak, nonatomic) IBOutlet UITextField *nametextfield; @property (weak, nonatomic) IBOutlet UITextField *mobiletextfield; - (IBAction)Save:(id)sender; 

和其他你做的一样

 **Detailviewcontroller.m** - (IBAction)Save:(id)sender { [self.delegate additem:self.nametextfield.text MOBILE:self.mobiletextfield.text]; }