最近课程设计写了个iOS通讯录,功能很简单,就是增删改查,再加上排序.
添加联系人后使用的是使用的代理传值,将联系人信息传到联系人列表.
添加联系人页面为AddViewController,联系人列表为ViewController
代理传值共有六个步骤:
1、声明协议(在AddViewController.h)
#import <UIKit/UIKit.h>
#pragma mark -- 第一步声明协议与方法:
@protocol WxDelegate <NSObject>
- (void)WxDelegate:(NSMutableDictionary *)myDic;
@end
@interface AddViewController : UIViewController
2、声明代理变量(在AddViewController.h)
#pragma mark -- 第二部声明代理人属性:
@property(nonatomic, copy)NSMutableArray *myDic;
@property(nonatomic, assign)id<WxDelegate>delegate;
3、设置代理(在ViewController里面点击添加联系人,所以在ViewController.m中设置)
- (void)addAction:(UIBarButtonItem *)sender
{
AddViewController *addContactVC = [AddViewController new];
addContactVC.delegate = self;
UINavigationController *addContactNC = [[UINavigationController alloc] initWithRootViewController:addContactVC];
[self presentViewController:addContactNC animated:YES completion:nil];
}
4、通过代理变量调用协议方法(让代理者开始执行协议,在AddViewController.m)
- (void)saveAction:(UIBarButtonItem *)sender
{
Node *head = [Node new];
Node *newNode = head;
if(nil == newNode) {
printf("失败");
exit(1);
}
// 获取输入内容
newNode.phone = self.textField3.text;
newNode.introduce = self.textField2.text;
newNode.name = self.textField.text;
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
// 获取输入内容
[dic setObject:newNode.phone forKey:@"phone"];
[dic setObject:newNode.introduce forKey:@"portrait"];
[dic setObject:newNode.name forKey:@"name"];
[self dismissViewControllerAnimated:YES completion:nil];
[self.delegate WxDelegate:dic];
[self.navigationController popViewControllerAnimated:YES];
}
5、在代理(ViewController.m)中遵守协议
@interface ViewController ()
<UITableViewDelegate,UITableViewDataSource,
UISearchBarDelegate,UISearchDisplayDelegate,WxDelegate>
6、代理实现协议方法(在ViewController.m里面)
- (void)WxDelegate:(NSMutableDictionary *)myDic
{
ContactModel *model = [[ContactModel alloc]initWithDic:myDic];
[self.dataArr addObject:model];
_rowArr = [ContactDataHelper getFriendListDataBy:self.dataArr];
_sectionArr = [ContactDataHelper getFriendListSectionBy:[_rowArr mutableCopy]];
//更新首页视图:
[self.tableView reloadData];
}
这样就可以把联系人信息传过去了
网友评论