码迷,mamicode.com
首页 > 其他好文 > 详细

通讯录(练习)

时间:2015-03-30 22:27:08      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

#import <UIKit/UIKit.h>

 

@interface RootViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>

 

@end

 

 

 

 

 

#import "RootViewController.h"

#import "SecondViewController.h"

 

@interface RootViewController () {

    NSMutableArray *dataArr;

    UITableView *myTableView;

}

 

@end

 

@implementation RootViewController

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

 

- (void)viewWillAppear:(BOOL)animated

{

    //从上一个界面返回的时候刷新tableView

    [myTableView reloadData];

}

 

- (void)presentToSecondViewController

{

    SecondViewController *svc = [[SecondViewController alloc]init];

    svc.arr = dataArr;

    [self presentViewController:svc animated:YES completion:nil];

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

    if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {

        self.automaticallyAdjustsScrollViewInsets = NO;

    }

    

    

    //取得NSUserDefaults的单例对象

    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

    

    //通过key找存放的数组

    dataArr = [ud objectForKey:@"vvvv"];

 

    if (!dataArr.count) {

        //如果没取到值,就重新开辟空间,并赋值

        //没找到的话就是空指针,空指针的任何操作都石沉大海

        dataArr = [[NSMutableArray alloc]init];

        for (int i = 0; i < 9; i++) {

            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"人物%d",i],@"personName",[NSString stringWithFormat:@"135000012%02d",i],@"phoneNumber", nil];

            

            //数组中放的是字典,每个字典有2个键值对,分别代表名字和电话号码

            [dataArr addObject:dic];

        }

        

        //通过key把数组放到ud里,并且同步

        [ud setObject:dataArr forKey:@"vvvv"];

        [ud synchronize];

    }

    

    myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, 320, 480-64) style:UITableViewStylePlain];

    myTableView.delegate = self;

    myTableView.dataSource = self;

    [self.view addSubview:myTableView];

    

    UIBarButtonItem *bbi = [[UIBarButtonItem alloc]initWithTitle:@"+" style:UIBarButtonItemStyleBordered target:self action:@selector(presentToSecondViewController)];

    self.navigationItem.rightBarButtonItem = bbi;

}

 

#pragma mark - tableView

 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 50;

}

 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return dataArr.count;

}

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"qwq"];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"qwq"];

    }

    

    cell.textLabel.text = [[dataArr objectAtIndex:indexPath.row] objectForKey:@"personName"];

    cell.detailTextLabel.text = [[dataArr objectAtIndex:indexPath.row] objectForKey:@"phoneNumber"];

    

    return cell;

}

 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    SecondViewController *svc = [[SecondViewController alloc]init];

    //数据源指针传过去

    svc.arr = dataArr;

    

    //点击的是第几个

    svc.i = indexPath.row;

    

    //用1代表push过去的,如果不传默认0的话就是弹过去的

    svc.j = 1;

    

    

    [self.navigationController pushViewController:svc animated:YES];

}

 

 

 

 

 

 

 

 

 

#import <UIKit/UIKit.h>

 

@interface SecondViewController : UIViewController

 

//接收上一个页面的数据源

@property (nonatomic,retain) NSMutableArray *arr;

 

//上一个页面点击的是第几个cell

@property (nonatomic,assign) int i;

 

//(如果是push过来的j就等于1,弹过来的就为0)

@property (nonatomic,assign) int j;

@end

 

 

 

 

 

 

 

 

 

#import "SecondViewController.h"

 

@interface SecondViewController () {

    UITextField *tf1;

    UITextField *tf2;

}

 

@end

 

@implementation SecondViewController

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

 

- (void)okbClick

{

    if (!tf1.text.length || !tf2.text.length) {

        UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"写字呀" message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];

        [av show];

        return;

    }

    

    //点击保存时如果2个tf里都有值就创建一个字典来记录信息

    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:tf1.text,@"personName",tf2.text,@"phoneNumber", nil];

    

    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

    

    if (self.j) {

        //如果是push过来的就删除原来的,添加新的(修改)

        [_arr removeObjectAtIndex:self.i];

        [_arr insertObject:dic atIndex:self.i];

        [ud setObject:_arr forKey:@"vvvv"];

        [ud synchronize];

        [self.navigationController popViewControllerAnimated:YES];

        

        return;

    }

    

    //如果是弹过来的,直接把字典添加到数据源里(新增)

    [_arr addObject:dic];

    [ud setObject:_arr forKey:@"vvvv"];

    [ud synchronize];

    [self dismissViewControllerAnimated:YES completion:nil];

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];

    

    tf1 = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 220, 30)];

    tf1.backgroundColor = [UIColor grayColor];

    [self.view addSubview:tf1];

    

    tf2 = [[UITextField alloc]initWithFrame:CGRectMake(50, 120, 220, 30)];

    tf2.backgroundColor = [UIColor grayColor];

    [self.view addSubview:tf2];

    

    //如果是push过来的话,就把传过来的名字和电话号码写上

    if (self.j) {

        tf1.text = [[self.arr objectAtIndex:_i] objectForKey:@"personName"];

        tf2.text = [[self.arr objectAtIndex:_i] objectForKey:@"phoneNumber"];;

    }

    

    UIButton *okb = [UIButton buttonWithType:UIButtonTypeCustom];

    okb.frame = CGRectMake(100, 170, 120, 40);

    [okb setTitle:@"save" forState:UIControlStateNormal];

    [okb setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

    [okb addTarget:self action:@selector(okbClick) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:okb];

    

}

通讯录(练习)

标签:

原文地址:http://www.cnblogs.com/-yun/p/4379164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!