大家每天都接触到电话,如今电话更是我们每天都离不开的通讯工具,随着科技的日益发达,电话已经不仅仅是能打电话这么简单了,今天我们就来一起看看电话中通讯录是如何用编程实现以及如何添加联系人的(删除联系人在以后会介绍).
首先创建通讯录工程 (以下均为在MRC模式下的编程)
在AppDelegate.m中引入ViewController并对window初始化,随即设置根视图控制器及导航控制器:
在View.Controller.m中实现已有联系人界面的实现:
//声明tableView属性
@property(nonatomic, retain)UITableView *tableView;
//声明数据源数组:(即显示已有联系人的数组)
@property(nonatomic, retain)NSMutableArray *dataSourceArray;
MRC编程所以需要释放:
在- (void)DidLoad中设置通讯录首页
创建导航栏:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Student information";
//修改字体:
[self.navigationController.navigationBar
setTitleTextAttributes:[NSDictionary
dictionaryWithObjectsAndKeys:[UIColor redColor],
NSForegroundColorAttributeName,[UIFont
fontWithName:@"Zapfino" size:20],NSFontAttributeName,
nil]];
//透明度:
self.navigationController.navigationBar.translucent = NO;
//滑动导航栏是否隐藏:
self.navigationController.hidesBarsOnSwipe = YES;
//添加右侧按钮,目的是到添加联系人界面并刷新tableView
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem
alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:@selector(didClickedRefreshButton:)];
}
为按钮添加点击方法:
- (void)didClickedRefreshButton:(UIBarButtonItem *)button
{
AddStudentViewController *avc =
[[AddStudentViewController alloc] init];
[self.navigationController pushViewController:avc
animated:YES];
}
运行如下图所示:
创建每一条cell的信息
签订UITableViewCell协议
@interface ViewController ()<UITableViewDelegate,
UITableViewDataSource>
//声明tableView属性
@property(nonatomic, retain)UITableView *tableView;
//声明数据源数组:(即显示已有联系人的数组)
@property(nonatomic, retain)NSMutableArray
*dataSourceArray;
添加已有的同学信息文件及头像文件到工程中:
在-(view)loadView中初始化tableView:
- (void)loadView
{
[super loadView];
self.dataSourceArray = [NSMutableArray arrayWithContentsOfFile:@"/Users/dllo/Desktop/GLX文件/UI/练习/Lianxi/通讯录/通讯录/Student副本.plist"];
# 注意:这里文件可直接拖动到方法中.
//初始化tableView:
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
//设置代理人:
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.rowHeight = 90.0f;
[self.view addSubview:_tableView];
[_tableView release];
}
完成协议必须实现的两个方法:
#pragma mark -- 必须实现的两个方法:
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return self.dataSourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
cell.imageView.image = [UIImage imageNamed:@"1.png"];
NSDictionary *dic = [self.dataSourceArray objectAtIndex:indexPath.row];
cell.textLabel.text = [dic valueForKey:@"name"];
return cell;
}
运行就可以得到当前的通讯录首页信息:(如下图所示)
新建联系人详情界面和添加新联系人界面
在ViewController.m中引入详情界面和添加新联系人界面的头文件:
#import "DetailViewController.h"
#import "AddStudentViewController.h"
在DetailViewController.h中声明属性
@property(nonatomic, retain)NSDictionary *detailDic;
在DetailViewController.m中声明相信信息属性:
@property(nonatomic, retain)UILabel *label;
@property(nonatomic, retain)UILabel *label1;
@property(nonatomic, retain)UILabel *label2;
@property(nonatomic, retain)UILabel *label3;
在DetailViewController.m中初始化个人详细信息:
- (void)loadView
{
[super loadView];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = [self.detailDic valueForKey:@"name"];
self.label = [[UILabel alloc] initWithFrame:CGRectMake(60, 100, 200, 50)];
self.label.backgroundColor = [UIColor yellowColor];
self.label.text = [self.detailDic valueForKey:@"gender"];
self.label.textAlignment = 1;
[self.view addSubview:_label];
[_label release];
self.label1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 160, 200, 50)];
self.label1.backgroundColor = [UIColor yellowColor];
self.label1.text = [self.detailDic valueForKey:@"age"];
self.label1.textAlignment = 1;
[self.view addSubview:_label1];
[_label1 release];
self.label2 = [[UILabel alloc] initWithFrame:CGRectMake(60, 220, 200, 50)];
self.label2.backgroundColor = [UIColor yellowColor];
self.label2.text = [self.detailDic valueForKey:@"number"];
self.label2.textAlignment = 1;
[self.view addSubview:_label2];
[_label2 release];
self.label3 = [[UILabel alloc] initWithFrame:CGRectMake(60, 280, 200, 50)];
self.label3.backgroundColor = [UIColor yellowColor];
self.label3.text = [self.detailDic valueForKey:@"hobby"];
self.label3.textAlignment = 1;
[self.view addSubview:_label3];
[_label3 release];
}
初始化详细信息后,我们需要把他添加到Cell的点击方法中,即利用属性传值来完成首页到详细信息界面之间的联系:
#pragma mark -- cell点击方法:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//设置详细信息界面的视图控制器
DetailViewController *dvc = [[DetailViewController alloc]
init];
//属性传值
dvc.detailDic = [self.dataSourceArray
objectAtIndex:indexPath.row];
//push到详情界面
[self.navigationController pushViewController:dvc
animated:YES];
}
完成后运行,在详情界面上点击cell,如点击"张晓军"就会push到这个界面:
接下来创建添加联系人的界面:
在AddStudentViewController.h中声明属性:
@property(nonatomic, retain)NSDictionary *detailDic;
在AddStudentViewController.m中声明textField和保存按钮属性:
@property(nonatomic, retain)UITextField *textField;
@property(nonatomic, retain)UITextField *textField1;
@property(nonatomic, retain)UITextField *textField2;
@property(nonatomic, retain)UITextField *textField3;
@property(nonatomic, retain)UITextField *textField4;
@property(nonatomic, retain)UIButton *button;
在viewDidLoad中简单设置添加联系人界面的基本信息:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"添加联系人";
}
初始化textField及其按钮:
(简单的初始化方法这里不过多解释)
- (void)loadView
{
[super loadView];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(80, 50, 215, 40)];
self.textField.placeholder = @"请输入姓名";
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.layer.borderWidth = 2.0;
self.textField.layer.backgroundColor = [UIColor greenColor].CGColor;
self.textField.textColor = [UIColor redColor];
self.textField.textAlignment = 0;
self.textField.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
[self.view addSubview:_textField];
[_textField release];
self.textField1 = [[UITextField alloc] initWithFrame:CGRectMake(80, 100, 215, 40)];
self.textField1.placeholder = @"请输入性别";
self.textField1.borderStyle = UITextBorderStyleRoundedRect;
self.textField1.layer.borderWidth = 2.0;
self.textField1.layer.backgroundColor = [UIColor greenColor].CGColor;
self.textField1.textColor = [UIColor redColor];
self.textField1.textAlignment = 0;
self.textField1.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
[self.view addSubview:_textField1];
[_textField1 release];
self.textField2 = [[UITextField alloc] initWithFrame:CGRectMake(80, 150, 215, 40)];
self.textField2.placeholder = @"请输入年龄";
self.textField2.borderStyle = UITextBorderStyleRoundedRect;
self.textField2.layer.borderWidth = 2.0;
self.textField2.layer.backgroundColor = [UIColor greenColor].CGColor;
self.textField2.textColor = [UIColor redColor];
self.textField2.textAlignment = 0;
self.textField2.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
[self.view addSubview:_textField2];
[_textField2 release];
self.textField3 = [[UITextField alloc] initWithFrame:CGRectMake(80, 200, 215, 40)];
self.textField3.placeholder = @"请输入学号";
self.textField3.borderStyle = UITextBorderStyleRoundedRect;
self.textField3.layer.borderWidth = 2.0;
self.textField3.layer.backgroundColor = [UIColor greenColor].CGColor;
self.textField3.textColor = [UIColor redColor];
self.textField3.textAlignment = 0;
self.textField3.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
[self.view addSubview:_textField3];
[_textField3 release];
self.textField4 = [[UITextField alloc] initWithFrame:CGRectMake(80, 250, 215, 40)];
self.textField4.placeholder = @"请输入爱好";
self.textField4.borderStyle = UITextBorderStyleRoundedRect;
self.textField4.layer.borderWidth = 2.0;
self.textField4.layer.backgroundColor = [UIColor greenColor].CGColor;
self.textField4.textColor = [UIColor redColor];
self.textField4.textAlignment = 0;
self.textField4.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
[self.view addSubview:_textField4];
[_textField4 release];
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
self.button.backgroundColor = [UIColor redColor];
self.button.frame = CGRectMake(10, 10, 50, 40);
[self.button setTitle:@"保存" forState:UIControlStateNormal];
self.button.layer.borderWidth = 2.0;
self.button.layer.borderColor = [UIColor yellowColor].CGColor;
self.button.layer.cornerRadius = 10;
[self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_button];
}
运行点击首页"+"按钮来到添加联系人界面:
为"保存"按钮添加点击方法:(点击保存让添加的联系人及信息同步到通讯录首页和个人详情界面)
这里最好用协议方法进行传值,所以我们先在AddStudentViewController.h中声明协议:(协议传值6步走)
#import <UIKit/UIKit.h>
#pragma mark -- 第一步声明协议:
@protocol MyDelegate <NSObject>
- (void)MyDelegate:(NSMutableDictionary *)myDic;
@end
@interface AddStudentViewController : UIViewController
#pragma mark -- 第二部声明代理人属性:
@property(nonatomic, copy)NSMutableArray *myDic;
@property(nonatomic, assign)id<MyDelegate>delegate;
@end
#设置保存按钮的点击方法
- (void)didClickButton:(UIButton *)button
{
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:self.textField.text forKey:@"name"];
[dic setObject:self.textField1.text forKey:@"gender"];
[dic setObject:self.textField2.text forKey:@"age"];
[dic setObject:self.textField3.text forKey:@"number"];
[dic setObject:self.textField4.text forKey:@"hobby"];
#pragma mark -- 第三步命令代理人执行协议方法:
[self.delegate MyDelegate:dic];
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -- 第四部在ViewController.m中签协议:
@interface ViewController ()<UITableViewDelegate,
UITableViewDataSource, MyDelegate>
#pragma mark -- 第五步在前面添加联系人的按钮添加方法里设
置代理人:
- (void)didClickedRefreshButton:(UIBarButtonItem *)button
{
AddStudentViewController *avc = [[AddStudentViewController alloc] init];
//设置代理人
avc.delegate = self;
[self.navigationController pushViewController:avc animated:YES];
//刷新tableView ,重新走一遍代理方法,为tableView重新赋值
[self.tableView reloadData];
}
#pragma mark -- 第六步实现协议方法:
- (void)MyDelegate:(NSMutableDictionary *)myDic
{
[self.dataSourceArray addObject:myDic];
//更新首页视图:
[self.tableView reloadData];
}