[英]how to use delegate to pass data to from one View Controller to another?
[英]How to pass message using delegates from one view controller to another using delegate?
主故事板图像我有一个名为“ Contacttableviewcontroller”的表视图控制器和一个名为“ Detailviewcontroller”的视图控制器。 我的View Controller中有2个文本字段,用于提供联系人姓名和电话号码。 我有一个要保存的按钮。 当我单击该按钮时,它应该在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:
按钮上。 您可以通过情节提要或以编程方式附加它。 要通过情节提要进行操作,请为您的按钮命名为saveButton(不是强制性的),然后像往常一样通过ctrl拖动将其附加。
确保您通过情节提要板正确连接了所有IBOutlets。
PS:我已经使用正确的命名约定更新了变量的名称。 在命名变量时,还应遵循驼峰式大小写约定。
这是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
现在,如果在动作方法中放置一个断点,就会看到它正在被调用。 您会看到一行额外的代码-
[self.navigationController popToRootViewControllerAnimated:YES];
-确保按下保存按钮时,它不仅发送数据,而且还返回给TableView Controller显示结果。
现在,您需要确保您的DetailViewController知道谁将实现其委托。 因此,无论您在何处在初始化TableViewViewController的ContactTableViewController中,都必须将其委托分配给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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.