#import "MainViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate,SecondViewControllerDelegate,ThirdViewControllerDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *stuArr;
@property(nonatomic, retain)NSMutableDictionary *stuDic;
@end
@implementation MainViewController
- (void)dealloc
{
[_stuArr release];
[_stuDic release];
[_tableView release];
[super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self createData];
}
return self;
}
- (void)createData
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"StudentArr" ofType:@"plist"];
self.stuArr = [NSMutableArray arrayWithContentsOfFile:path];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor cyanColor];
self.navigationController.navigationBar.translucent = NO;
self.title = @"通讯录";
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT -64) style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.rowHeight = 100;
[self.view addSubview:self.tableView];
[_tableView release];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"3.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction)];
}
// 添加创建按钮
- (void)rightBarButtonAction
{
ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
[self.navigationController pushViewController:thirdVC animated:YES];
thirdVC.delegate = self;
[thirdVC release];
}
// 创建的传值
- (void)changeValue:(NSMutableDictionary *)dic
{
[self.stuArr addObject:dic];
[self.tableView reloadData];
}
// tableview协议实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.stuArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *resuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resuse];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:resuse] autorelease];
}
self.stuDic = self.stuArr[indexPath.row];
cell.textLabel.text = self.stuDic[@"name"];
cell.detailTextLabel.text = self.stuDic[@"phone"];
NSString *picstr = [NSString stringWithFormat:@"%ld.jpg",indexPath.row+1];
cell.imageView.image = [UIImage imageNamed:picstr];
return cell;
}
// 跳转下一界面(修改界面)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondViewController *secVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secVC animated:YES];
secVC.arr = self.stuArr;
secVC.num = indexPath.row;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",indexPath.row +1]]];
imageView.frame = CGRectMake(10, 50, WIDTH/2, HEIGHT - 150);
[secVC.view addSubview:imageView];
[imageView release];
self.stuDic = self.stuArr[indexPath.row];
secVC.delegate = self;
[secVC release];
}
// 修改传值
- (void)modifyValue:(NSMutableDictionary *)dic atindex:(NSInteger)num
{
[self.stuArr replaceObjectAtIndex:num withObject:dic];
[self.tableView reloadData];
}
#import <UIKit/UIKit.h>
@protocol SecondViewControllerDelegate <NSObject>
- (void)modifyValue:(NSMutableDictionary *)dic atindex:(NSInteger)num;
@end
@interface SecondViewController : UIViewController
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, assign)NSInteger num;
@property(nonatomic, assign)id<SecondViewControllerDelegate>delegate;
@end
#import "SecondViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface SecondViewController ()<UITextFieldDelegate>
@property(nonatomic, retain)UITextField *namefield;
@property(nonatomic, retain)UITextField *phonefield;
@property(nonatomic, retain)UITextField *sexfield;
@property(nonatomic, retain)UITextField *hobbyfield;
@property(nonatomic, retain)UIButton *button;
@property(nonatomic, retain)NSDictionary *dic;
@property(nonatomic, retain)NSMutableDictionary *modifydic;
@end
@implementation SecondViewController
- (void)dealloc
{
[_modifydic release];
[_dic release];
[_arr release];
[_namefield release];
[_phonefield release];
[_sexfield release];
[_hobbyfield release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"联系人";
self.dic = self.arr[self.num];
self.namefield = [[UITextField alloc] initWithFrame:CGRectMake(210, 50, 150, 40)];
self.namefield.layer.cornerRadius = 10;
self.namefield.layer.borderWidth = 1;
self.namefield.text = self.dic[@"name"];
self.namefield.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.namefield];
[_namefield release];
self.sexfield = [[UITextField alloc] initWithFrame:CGRectMake(210, 150, 150, 40)];
self.sexfield.layer.cornerRadius = 10;
self.sexfield.layer.borderWidth = 1;
self.sexfield.text = self.dic[@"sex"];
self.sexfield.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.sexfield];
[_sexfield release];
self.hobbyfield = [[UITextField alloc] initWithFrame:CGRectMake(210, 250, 150, 40)];
self.hobbyfield.layer.cornerRadius = 10;
self.hobbyfield.layer.borderWidth = 1;
self.hobbyfield.text = self.dic[@"hobby"];
self.hobbyfield.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.hobbyfield];
[_hobbyfield release];
self.phonefield = [[UITextField alloc] initWithFrame:CGRectMake(210, 350, 150, 40)];
self.phonefield.layer.cornerRadius = 10;
self.phonefield.layer.borderWidth = 1;
self.phonefield.text = self.dic[@"phone"];
self.phonefield.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.phonefield];
[_phonefield release];
self.namefield.delegate = self;
self.hobbyfield.delegate = self;
self.phonefield.delegate = self;
self.sexfield.delegate = self;
self.button = [UIButton buttonWithType:UIButtonTypeSystem];
self.button.frame = CGRectMake(230, 450 , 100, 40);
self.button.layer.borderWidth = 1;
self.button.layer.cornerRadius = 10;
[self.button setTitle:@"完成" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
}
// 传值
- (void)click:(UIButton *)button
{
self.modifydic = [NSMutableDictionary dictionary];
[self.modifydic setObject:self.namefield.text forKey:@"name"];
[self.modifydic setObject:self.phonefield.text forKey:@"phone"];
[self.modifydic setObject:self.hobbyfield.text forKey:@"hobby"];
[self.modifydic setObject:self.sexfield.text forKey:@"sex"];
[self.navigationController popToRootViewControllerAnimated:YES];
[self.delegate modifyValue:self.modifydic atindex:self.num];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[_namefield resignFirstResponder];
[_phonefield resignFirstResponder];
[_sexfield resignFirstResponder];
[_hobbyfield resignFirstResponder];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.frame.origin.y >self.view.frame.size.height/2) {
CGFloat distance = textField.frame.origin.y - self.view.frame.size.height/2;
self.view.center = CGPointMake(self.view.center.x, self.view.center.y - distance);
}
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if (textField.frame.origin.y >self.view.frame.size.height/2) {
CGFloat distance = textField.frame.origin.y - self.view.frame.size.height/2;
self.view.center = CGPointMake(self.view.center.x, self.view.center.y + distance);
}
return YES;
}
#import <UIKit/UIKit.h>
@protocol ThirdViewControllerDelegate <NSObject>
- (void)changeValue:(NSMutableDictionary *)dic;
@end
@interface ThirdViewController : UIViewController
@property(nonatomic, assign)id<ThirdViewControllerDelegate>delegate;
@end
#import "ThirdViewController.h"
@interface ThirdViewController ()<UITextFieldDelegate>
@property(nonatomic, retain)UITextField *namefield;
@property(nonatomic, retain)UITextField *phonefield;
@property(nonatomic, retain)UITextField *sexfield;
@property(nonatomic, retain)UITextField *hobbyfield;
@property(nonatomic, retain)UIButton *button;
@property(nonatomic, retain)NSMutableDictionary *adddic;
@end
@implementation ThirdViewController
- (void)dealloc
{
[_adddic release];
[_button release];
[_namefield release];
[_sexfield release];
[_phonefield release];
[_hobbyfield release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.namefield = [[UITextField alloc] initWithFrame:CGRectMake(100, 50, 200, 40)];
self.namefield.layer.cornerRadius = 10;
self.namefield.layer.borderWidth = 1;
self.namefield.placeholder = @"姓名";
self.namefield.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.namefield];
[_namefield release];
self.sexfield = [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 200, 40)];
self.sexfield.layer.cornerRadius = 10;
self.sexfield.layer.borderWidth = 1;
self.sexfield.placeholder = @"性别";
self.sexfield.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.sexfield];
[_sexfield release];
self.hobbyfield = [[UITextField alloc] initWithFrame:CGRectMake(100, 250, 200, 40)];
self.hobbyfield.layer.cornerRadius = 10;
self.hobbyfield.layer.borderWidth = 1;
self.hobbyfield.placeholder = @"爱好";
self.hobbyfield.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.hobbyfield];
[_hobbyfield release];
self.phonefield = [[UITextField alloc] initWithFrame:CGRectMake(100, 350, 200, 40)];
self.phonefield.layer.cornerRadius = 10;
self.phonefield.layer.borderWidth = 1;
self.phonefield.placeholder = @"电话";
self.phonefield.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.phonefield];
[_phonefield release];
self.namefield.delegate = self;
self.hobbyfield.delegate = self;
self.phonefield.delegate = self;
self.sexfield.delegate = self;
self.button = [UIButton buttonWithType:UIButtonTypeSystem];
self.button.frame = CGRectMake(150, 450 , 100, 40);
self.button.layer.borderWidth = 1;
self.button.layer.cornerRadius = 10;
[self.button setTitle:@"完成" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
}
- (void)click:(UIButton *)button
{
self.adddic = [NSMutableDictionary dictionary];
[self.adddic setObject:self.namefield.text forKey:@"name"];
[self.adddic setObject:self.phonefield.text forKey:@"phone"];
[self.adddic setObject:self.hobbyfield.text forKey:@"hobby"];
[self.adddic setObject:self.sexfield.text forKey:@"sex"];
[self.navigationController popToRootViewControllerAnimated:YES];
[self.delegate changeValue:self.adddic];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[_namefield resignFirstResponder];
[_phonefield resignFirstResponder];
[_sexfield resignFirstResponder];
[_hobbyfield resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.frame.origin.y >self.view.frame.size.height/2) {
CGFloat distance = textField.frame.origin.y - self.view.frame.size.height/2;
self.view.center = CGPointMake(self.view.center.x, self.view.center.y - distance);
}
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if (textField.frame.origin.y >self.view.frame.size.height/2) {
CGFloat distance = textField.frame.origin.y - self.view.frame.size.height/2;
self.view.center = CGPointMake(self.view.center.x, self.view.center.y + distance);
}
return YES;
}