通讯录 用沙盒实现的增删改查

源文件支持交流修改使用

开发语言:

分类:其他系统

    分享到:

源码搜索

  • 详情介绍 - 【通讯录 用沙盒实现的增删改查】
#import "editViewController.h"
 
@interface editViewController ()
 
@property (strong,nonatomic) UIImageView *imgView;
@property (strong,nonatomic) UITextField *nameTextField;
@property (strong,nonatomic) UITextField *genderTextField;
@property (strong,nonatomic) UITextField *phoneNumberTextField;
@property (strong,nonatomic) UITextField *hobbyTextField;
@property (strong,nonatomic) UITextField *QQTextField;
@property (strong,nonatomic) NSMutableDictionary *dict;
 
@end
 
@implementation editViewController
 
#define kWidth self.view.bounds.size.width
#define kHeight self.view.bounds.size.height
#define kGap kWidth/20
 
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden=YES; 
    [self setupAction];
     
     
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tap1.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap1];
     
 
     
}
 
-(void)viewTapped:(UITapGestureRecognizer*)tap1
{
     
    [self.view endEditing:YES];
}
 
- (void)setupAction
{
     
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, kGap, kWidth/2, 2.5*kGap);
    [self.view addSubview:backButton];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [backButton setTitle:@"取消" forState:UIControlStateNormal];
    backButton.backgroundColor = [UIColor grayColor];
     
    UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    saveButton.frame = CGRectMake(kWidth/2 + 2, kGap, kWidth/2 - 1, 2.5*kGap);
    [self.view addSubview:saveButton];
    [saveButton addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    saveButton.backgroundColor = [UIColor grayColor];
     
     
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(kGap, CGRectGetMaxY(backButton.frame) + kGap/5, kWidth - 2*kGap, 2*kGap)];
    label1.text = @"手机 联系人";
    label1.font = [UIFont systemFontOfSize:18];
    //label1.layer.borderWidth = 1;
    label1.layer.cornerRadius = 5;
    label1.layer.masksToBounds = YES;
    label1.backgroundColor = [UIColor colorWithRed:222/255.0 green:220/255.0 blue:222/255.0 alpha:1];
    [self.view addSubview:label1];
     
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMinX(label1.frame), CGRectGetMaxY(label1.frame) + kGap/5, kGap*7.5, kGap*7.5)];
    imgView.image = [UIImage imageNamed:@"1.jpg"];
    [self.view addSubview:imgView];
     
    self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imgView.frame) + kGap, CGRectGetMaxY(label1.frame) + kGap/5, kWidth/2 - kGap, kGap * 2)];
    self.nameTextField.placeholder = @"姓名";
    self.nameTextField.layer.borderWidth = 1;
    self.nameTextField.layer.borderColor = [UIColor colorWithRed:126/255.0 green:167/255.0 blue:167/255.0 alpha:1].CGColor;
    [self.view addSubview:self.nameTextField];
    self.nameTextField.layer.cornerRadius = 5;
    self.nameTextField.layer.masksToBounds = YES;
     
    self.genderTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imgView.frame) + kGap, CGRectGetMaxY(self.nameTextField.frame) +0.6*kGap, kWidth/2 - kGap, kGap * 2)];
    self.genderTextField.placeholder = @"性别";
    self.genderTextField.layer.borderWidth = 1;
    self.genderTextField.layer.borderColor = [UIColor colorWithRed:126/255.0 green:167/255.0 blue:167/255.0 alpha:1].CGColor;
    [self.view addSubview:self.genderTextField];
    self.genderTextField.layer.cornerRadius = 5;
    self.genderTextField.layer.masksToBounds = YES;
     
    self.QQTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imgView.frame) + kGap, CGRectGetMaxY(self.genderTextField.frame) +0.7*kGap, kWidth/2 - kGap, kGap * 2)];
    self.QQTextField.placeholder = @"QQ";
    self.QQTextField.layer.borderWidth = 1;
    self.QQTextField.layer.borderColor = [UIColor colorWithRed:126/255.0 green:167/255.0 blue:167/255.0 alpha:1].CGColor;
    [self.view addSubview:self.QQTextField];
    self.QQTextField.layer.cornerRadius = 5;
    self.QQTextField.layer.masksToBounds = YES;
     
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(kGap, CGRectGetMaxY(imgView.frame) + kGap/5, kWidth - 2*kGap, 2*kGap)];
    label2.text = @"手机号";
    label2.font = [UIFont systemFontOfSize:18];
    label2.layer.cornerRadius = 5;
    label2.layer.masksToBounds = YES;
    label2.backgroundColor = [UIColor colorWithRed:222/255.0 green:220/255.0 blue:222/255.0 alpha:1];
    [self.view addSubview:label2];
     
    UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMinX(label1.frame), CGRectGetMaxY(label2.frame) + kGap/5, kGap*3, kGap*3)];
    imgView1.image = [UIImage imageNamed:@"LS1.jpg"];
    [self.view addSubview:imgView1];
     
    self.phoneNumberTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imgView1.frame) + kGap, CGRectGetMaxY(label2.frame) +0.7*kGap, kWidth * 0.7, kGap * 2)];
    self.phoneNumberTextField.placeholder = @"手机号";
    self.phoneNumberTextField.layer.borderWidth = 1;
    self.phoneNumberTextField.layer.borderColor = [UIColor colorWithRed:126/255.0 green:167/255.0 blue:167/255.0 alpha:1].CGColor;
    [self.view addSubview:self.phoneNumberTextField];
    self.phoneNumberTextField.layer.cornerRadius = 5;
    self.phoneNumberTextField.layer.masksToBounds = YES;
     
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(kGap, CGRectGetMaxY(imgView1.frame) + kGap/5, kWidth - 2*kGap, 2*kGap)];
    label3.text = @"爱好";
    label3.font = [UIFont systemFontOfSize:18];
    label3.layer.cornerRadius = 5;
    label3.layer.masksToBounds = YES;
    label3.backgroundColor = [UIColor colorWithRed:222/255.0 green:220/255.0 blue:222/255.0 alpha:1];
    [self.view addSubview:label3];
     
    self.hobbyTextField = [[UITextField alloc] initWithFrame:CGRectMake(kGap, CGRectGetMaxY(label3.frame) +0.7*kGap, kWidth - 2*kGap, kHeight * 2/5 - 3*kGap)];
    self.hobbyTextField.layer.borderWidth = 1;
    self.hobbyTextField.layer.borderColor = [UIColor colorWithRed:126/255.0 green:167/255.0 blue:167/255.0 alpha:1].CGColor;
    [self.view addSubview:self.hobbyTextField];
    self.hobbyTextField.layer.cornerRadius = 5;
    self.hobbyTextField.layer.masksToBounds = YES;
     
}
 
- (void)write
{
 
    
    Person *person = [Person new];
    person.name = self.nameTextField.text;
    person.gender = self.genderTextField.text;
    person.QQNumber = self.QQTextField.text;
    person.phoneNumber = self.phoneNumberTextField.text;
    person.key = [self.nameTextField.text uppercasePinYinFirstLetter];
     
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *personsArrPath = [path stringByAppendingString:@"/personsDic.plist"];
    NSMutableDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithFile:personsArrPath];
     
    if ([dict allKeys]) {
        self.dict = dict;
    }
     
    if (self.dict[person.key] == nil) {
         
        NSMutableArray *arr = [NSMutableArray arrayWithObject:person];
        [self.dict setObject:arr forKey:person.key];
         
    }else
    {
         
        [self.dict[person.key] addObject:person];
         
    }
     
    [NSKeyedArchiver archiveRootObject:self.dict toFile:personsArrPath];
    [self.dict writeToFile:personsArrPath atomically:YES];
     
}
 
 
- (void)backAction
{
 
    [self.navigationController popViewControllerAnimated:YES];
 
 
}
 
- (void)saveAction
{
 
    if ([self.nameTextField.text isEqualToString:@""]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入完整信息" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
         
    }else
    {
        if (self.indexPath) {
             
            [self editAction];
            [self.navigationController popToRootViewControllerAnimated:YES];
             
        }else
        {
         
            [self write];
            [self.navigationController popViewControllerAnimated:YES];
             
        }
         
         
    }
 
}
 
- (void)viewWillAppear:(BOOL)animated
{
 
    if (self.indexPath) {
     
        self.nameTextField.text = self.person.name;
        self.phoneNumberTextField.text = self.person.phoneNumber;
        self.QQTextField.text = self.person.QQNumber;
        self.genderTextField.text = self.person.gender;
         
    }
     
 
}
 
- (void)editAction
{
 
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *personsArrPath = [path stringByAppendingString:@"/personsDic.plist"];
    NSMutableDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithFile:personsArrPath];
    NSArray *array = [dict allKeys];
    Person *person = dict[array[self.indexPath.section]][self.indexPath.row];
    person.name = self.nameTextField.text;
    person.gender = self.genderTextField.text;
    person.phoneNumber = self.phoneNumberTextField.text;
    person.QQNumber = self.QQTextField.text;
    person.key = [self.nameTextField.text uppercasePinYinFirstLetter];
    [NSKeyedArchiver archiveRootObject:dict toFile:personsArrPath];
    [dict writeToFile:personsArrPath atomically:YES];
 
}
 
 
- (void)viewWillDisappear:(BOOL)animated
{
     
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden=NO;
     
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
- (NSMutableDictionary *)dict
{
    if (_dict == nil) {
        _dict = [NSMutableDictionary dictionary];
    }
    return _dict;
 
}

特别说明:
☉如果服务器暂不能下载请稍后重试!总是不能下载,请联系客服QQ:597260440,谢谢合作!
☉本站所有源码仅供学习与参考,请勿用于商业用途。
模板说明:
源码-通讯录 用沙盒实现的增删改查,其版权归原作者所有,如损害你的权益请联系客服QQ:597260440给予处理。

本文永久链接:http://code.fangbianle.com/goods/show-5196.html

相关源码

在线客服
热线电话

备案号:豫ICP备14017793号-2编辑热线:0371-56691758

Copyight@2014-2018 All Rights Reserved 页面加载耗时0.101秒