标签:
//
// 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 ()<UITableViewDataSource,UITableViewDelegate,SecondViewController,deleteController,addNewController>
@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 <UIKit/UIKit.h>
//1.声明一份协议,为了从第二页向第一页传值
@protocol SecondViewController <NSObject>
-(void)change:(NSMutableArray *)array
index:(NSInteger)index;
@end
//声明第二份协议
@protocol deleteController <NSObject>
-(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)id<SecondViewController>delegate;
//设置第二份协议代理人属性
@property(nonatomic,assign)id<deleteController>delegate1;
@end
//
// SecondViewController.m
// 电话本
//
// Created by dllo on 15/8/8.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
//
#import "SecondViewController.h"
@interface SecondViewController ()<UITextFieldDelegate>
#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 <UIKit/UIKit.h>
//声明协议
@protocol addNewController <NSObject>
-(void)addPerson:(NSMutableArray *)arr;
@end
@interface AddViewController : UIViewController
@property(nonatomic,retain)NSMutableArray *newArr;
//代理人属性
@property(nonatomic,retain)id<addNewController>delegate2;
@end
//
// AddViewController.m
// 电话本
//
// Created by dllo on 15/8/8.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
//
#import "AddViewController.h"
@interface AddViewController ()<UITextFieldDelegate>
#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
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/cheng_xiansheng/article/details/47393357