//
// MainViewController.m
// 電話本
//
// Created by dllo on 15/8/7.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
//
#import MainViewController.h
#import SecondViewController.h
#import AddViewController.h
//4.簽訂協議
@interface MainViewController ()
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,retain)NSMutableArray *imageArr;
@end
@implementation MainViewController
-(void)dealloc
{
[_arr release];
[_imageArr release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//建立一個數組,存放student數據
NSString *path=[[NSBundle mainBundle] pathForResource:@StudentArr ofType:@plist];
self.arr = [[NSMutableArray alloc] initWithContentsOfFile:path];
// NSLog(@%@,self.arr);
//設置電話薄第一頁
self.view.backgroundColor=[UIColor cyanColor];
self.navigationController.navigationBar.translucent=NO;
self.navigationItem.title=@電話簿;
//創建tableView
UITableView *tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
tableView.backgroundColor=[UIColor cyanColor];
[self.view addSubview:tableView];
[tableView release];
//行高
tableView.rowHeight=100;
//兩個協議的代理人
tableView.delegate=self;
tableView.dataSource=self;
tableView.tag=1000;
//用來存放所有圖片的數組
self.imageArr=[[NSMutableArray alloc] init];
for (int i = 1;i <= 22 ; i++) {
NSString *imageName=[NSString stringWithFormat:@%d.jpg,i];
UIImage *image=[UIImage imageNamed:imageName];
[self.imageArr addObject:image];
}
//創建添加符號
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNew:)];
}
//指定分區設置行數
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//和數組元素個數一樣
return self.arr.count;
}
//顯示數據
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//節約空間
static NSString *reuse=@reuse;
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
//創建cell
if (!cell) {
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
}
//cellde顯示
cell.textLabel.text=self.arr[indexPath.row][0];
cell.detailTextLabel.text=self.arr[indexPath.row][3];
cell.imageView.image=self.imageArr[indexPath.row];
return cell;
}
//點擊觸發的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondViewController *secVC=[[SecondViewController alloc] init];
secVC.detailArr=self.arr[indexPath.row];
secVC.image = self.imageArr[indexPath.row];
[self.navigationController pushViewController:secVC animated:YES];
[secVC release];
secVC.index=indexPath.row;
//5.設置代理人
secVC.delegate=self;
secVC.delegate1=self;
}
//6.實現方法
-(void)change:(NSMutableArray *)array index:(NSInteger)index
{
[self.arr replaceObjectAtIndex:index withObject:array];
//刷新
[(UITableView *)[self.view viewWithTag:1000] reloadData];
}
//實現刪除
-(void)delete:(NSInteger)index
{
[self.arr removeObjectAtIndex:index];
[self.imageArr removeObjectAtIndex:index];
//刷新
[(UITableView *)[self.view viewWithTag:1000] reloadData];
}
//實現進入添加頁面
-(void)addNew:(UIBarButtonItem *)bar
{
AddViewController *addVC=[[AddViewController alloc] init];
[self.navigationController pushViewController:addVC animated:YES];
[addVC release];
addVC.delegate2=self;
}
//添加方法的實現
-(void)addPerson:(NSMutableArray *)arr
{
[self.arr addObject:arr];
[self.imageArr addObject:[UIImage imageNamed:@20131205172421_QKF4K.thumb.600_0.jpg]];
[(UITableView *)[self.view viewWithTag:1000] reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// SecondViewController.h
// 電話本
//
// Created by dllo on 15/8/8.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
//
#import
//1.聲明一份協議,為了從第二頁向第一頁傳值
@protocol SecondViewController
-(void)change:(NSMutableArray *)array
index:(NSInteger)index;
@end
//聲明第二份協議
@protocol deleteController
-(void)delete:(NSInteger)index;
@end
@interface SecondViewController : UIViewController
@property(nonatomic,retain)NSMutableArray *detailArr;
@property(nonatomic,retain)UIImage *image;
@property(nonatomic,assign)NSInteger index;
//2.設置代理的屬性
@property(nonatomic,retain)iddelegate;
//設置第二份協議代理人屬性
@property(nonatomic,assign)iddelegate1;
@end
//
// SecondViewController.m
// 電話本
//
// Created by dllo on 15/8/8.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
//
#import SecondViewController.h
@interface SecondViewController ()
#define HEIGHT self.view.frame.size.height
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor cyanColor];
//顯示大圖
UIImageView *imageView=[[UIImageView alloc] initWithImage:self.image];
imageView.frame=CGRectMake(0, 0, self.view.frame.size.width, 320);
[self.view addSubview:imageView];
[imageView release];
//建立四個label
for (int i = 1; i < 5; i++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 330 + i * 50, 100, 40)];
label.layer.borderWidth = 1;
label.layer.cornerRadius= 5;
[self.view addSubview:label];
[label release];
label.tag = i;
}
//四個label顯示對應的文字
UILabel *label1 =(UILabel *)[self.view viewWithTag:1];
label1.text=@姓名;
UILabel *label2 =(UILabel *)[self.view viewWithTag:2];
label2.text=@性別;
UILabel *label3 =(UILabel *)[self.view viewWithTag:3];
label3.text=@愛好;
UILabel *label4 =(UILabel *)[self.view viewWithTag:4];
label4.text=@電話;
//建立四個textfield
for (int i = 1; i < 5; i ++) {
UITextField *textfield =[[UITextField alloc] initWithFrame:CGRectMake(180, 330 + i * 50, 150 ,40 )];
textfield.layer.borderWidth=1;
textfield.layer.cornerRadius=5;
[self.view addSubview:textfield];
[textfield release];
textfield.tag=10+i;
textfield.delegate=self;
}
//顯示對應在數組的內容
UITextField *textfield1=(UITextField *)[self.view viewWithTag:11];
textfield1.text=self.detailArr[0];
UITextField *textfield2=(UITextField *)[self.view viewWithTag:12];
textfield2.text=self.detailArr[1];
UITextField *textfield3=(UITextField *)[self.view viewWithTag:13];
textfield3.text=self.detailArr[2];
UITextField *textfield4=(UITextField *)[self.view viewWithTag:14];
textfield4.text=self.detailArr[3];
//創建保存button
UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeSystem];
saveButton.frame=CGRectMake(70, 340, 60, 30);
saveButton.layer.borderWidth=1;
saveButton.layer.cornerRadius=5;
[saveButton setTitle:@保存 forState:UIControlStateNormal];
[self.view addSubview:saveButton];
//點擊方法
[saveButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//刪除
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeSystem];
deleteButton.frame=CGRectMake(200, 340, 60, 30);
deleteButton.layer.borderWidth=1;
deleteButton.layer.cornerRadius=5;
[deleteButton setTitle:@刪除 forState:UIControlStateNormal];
[self.view addSubview:deleteButton];
//點擊方法
[deleteButton addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
}
//鍵盤上移
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.frame.origin.y > HEIGHT/2) {
CGFloat height=textField.frame.origin.y - HEIGHT / 2;
self.view.center =CGPointMake(self.view.center.x, self.view.center.y-height);
}
return YES;
}
//回收鍵盤
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
// 編譯結束,回到原位
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if (textField.frame.origin.y > HEIGHT / 2) {
CGFloat height =textField.frame.origin.y- HEIGHT / 2;
self.view.center=CGPointMake(self.view.center.x, self.view.center.y + height);
}
return YES;
}
//點擊方法的實現
-(void)click:(UIButton *)button
{
//返回前一頁
[self.navigationController popToRootViewControllerAnimated:YES];
//封裝新值
self.detailArr[0]=((UITextField *)[self.view viewWithTag:11]).text;
self.detailArr[1]=((UITextField *)[self.view viewWithTag:12]).text;
self.detailArr[2]=((UITextField *)[self.view viewWithTag:13]).text;
self.detailArr[3]=((UITextField *)[self.view viewWithTag:14]).text;
//3.實現傳值效果
[self.delegate change:self.detailArr index:self.index];
}
//刪除點擊方法
-(void)deleteClick:(UIButton *)button
{
//返回前一頁
[self.navigationController popToRootViewControllerAnimated:YES];
//傳值效果
[self.delegate1 delete:self.index];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// AddViewController.h
// 電話本
//
// Created by dllo on 15/8/8.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
//
#import
//聲明協議
@protocol addNewController
-(void)addPerson:(NSMutableArray *)arr;
@end
@interface AddViewController : UIViewController
@property(nonatomic,retain)NSMutableArray *newArr;
//代理人屬性
@property(nonatomic,retain)iddelegate2;
@end
//
// AddViewController.m
// 電話本
//
// Created by dllo on 15/8/8.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
//
#import AddViewController.h
@interface AddViewController ()
#define HEIGHT self.view.frame.size.height
@end
@implementation AddViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@42a00127b81a98150e121cdf3c42c756.jpg]];
self.view.alpha=0.5;
self.navigationItem.title=@新建聯系人;
//建立四個label
for (int i = 1; i < 5; i++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 330 + i * 50, 100, 40)];
label.layer.borderWidth = 1;
label.layer.cornerRadius= 5;
[self.view addSubview:label];
[label release];
label.tag = i;
}
//四個label顯示對應的文字
UILabel *label1 =(UILabel *)[self.view viewWithTag:1];
label1.text=@姓名;
UILabel *label2 =(UILabel *)[self.view viewWithTag:2];
label2.text=@性別;
UILabel *label3 =(UILabel *)[self.view viewWithTag:3];
label3.text=@愛好;
UILabel *label4 =(UILabel *)[self.view viewWithTag:4];
label4.text=@電話;
//建立四個textfield
for (int i = 1; i < 5; i ++) {
UITextField *textfield =[[UITextField alloc] initWithFrame:CGRectMake(180, 330 + i * 50, 150 ,40 )];
textfield.layer.borderWidth=1;
textfield.layer.cornerRadius=5;
[self.view addSubview:textfield];
[textfield release];
textfield.tag=10+i;
textfield.delegate=self;
}
//創建新建button
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeSystem];
newButton.frame=CGRectMake(70, 340, 60, 30);
newButton.layer.borderWidth=1;
newButton.layer.cornerRadius=5;
[newButton setTitle:@新建 forState:UIControlStateNormal];
[self.view addSubview:newButton];
//點擊方法
[newButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//返回鍵
UIButton *returnButton =[UIButton buttonWithType:UIButtonTypeSystem];
returnButton.frame=CGRectMake(230, 340, 60, 30);
returnButton.layer.borderWidth=1;
returnButton.layer.cornerRadius=5;
[returnButton setTitle:@返回 forState:UIControlStateNormal];
[self.view addSubview:returnButton];
//點擊方法
[returnButton addTarget:self action:@selector(returnClick:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)click:(UIButton *)button
{
[self.navigationController popToRootViewControllerAnimated:YES];
//封裝
self.newArr=[[NSMutableArray alloc] init];
self.newArr[0]=((UITextField *)[self.view viewWithTag:11]).text;
self.newArr[1]=((UITextField *)[self.view viewWithTag:12]).text;
self.newArr[2]=((UITextField *)[self.view viewWithTag:13]).text;
self.newArr[3]=((UITextField *)[self.view viewWithTag:14]).text;
[self.delegate2 addPerson:self.newArr];
}
//返回
-(void)returnClick:(UIButton *)button
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
//鍵盤上移
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.frame.origin.y > HEIGHT/2) {
CGFloat height=textField.frame.origin.y - HEIGHT / 2;
self.view.center =CGPointMake(self.view.center.x, self.view.center.y-height);
}
return YES;
}
//回收鍵盤
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
// 編譯結束,回到原位
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if (textField.frame.origin.y > HEIGHT / 2) {
CGFloat height =textField.frame.origin.y- HEIGHT / 2;
self.view.center=CGPointMake(self.view.center.x, self.view.center.y + height);
}
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end