Xcode简易通讯录的实现——添加搜索功能

ViewController.m文件

#import "ViewController.h"
#import "Contact.h"

@interface ViewController ()
{
    NSString *_fileSavePath;
    NSMutableArray *_contactsArray;
    NSMutableArray *_contactArrayIndex;
    NSInteger _index;
}
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *genderTextField;
@property (weak, nonatomic) IBOutlet UITextField *companyTextField;
@property (weak, nonatomic) IBOutlet UITextField *occupationTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *emailTextField;
@property (weak, nonatomic) IBOutlet UITextField *wechatTextField;
@property (weak, nonatomic) IBOutlet UITextField *searchTextField;
@property (weak, nonatomic) IBOutlet UITextView *resultTextView;

@end

@implementation ViewController

#pragma mark 初始化路径和联系人数组
- (void)getPrepared{
    _fileSavePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/forContacts.plist"];
    NSLog(@"%@",_fileSavePath);
    _contactsArray = [NSMutableArray array];
}
//添加
- (IBAction)addButtonClicked:(id)sender {
    if (!_nameTextField.text) {
        NSLog(@"名字不能为空");
        return;
    }
    Contact *newContact = [[Contact alloc] initName:_nameTextField.text andGender:_genderTextField.text andCompany:_companyTextField.text andOccupation:_occupationTextField.text andPhone:_phoneTextField.text andEmail:_emailTextField.text andWechat:_wechatTextField.text];
    [_contactsArray addObject:newContact];
    [self saveContacts];
}
//上一个
- (IBAction)preButtonClicked:(id)sender {
    if (_contactsArray.count == 0) {
        return;
    }else{
         if (_index==0) {
        _index=[_contactsArray count];
    }
    _index=_index-1;
    Contact *newContact = [_contactsArray objectAtIndex:_index];
    _nameTextField.text = newContact.name;
    _genderTextField.text = newContact.gender;;
    _companyTextField.text = newContact.company;
    _occupationTextField.text = newContact.occupation;
    _phoneTextField.text = newContact.phone;
    _emailTextField.text = newContact.email;
    _wechatTextField.text = newContact.wechat;
    }
}
//下一个
- (IBAction)nextButtonClicked:(id)sender {
    if (_contactsArray.count <= 1) {
        return;
    }
    _index = _index + 1;
    if (_index==0) {
        _index=[_contactsArray count];
    }
    Contact *newContact = [_contactsArray objectAtIndex:_index];
    _nameTextField.text = newContact.name;
    _genderTextField.text = newContact.gender;;
    _companyTextField.text = newContact.company;
    _occupationTextField.text = newContact.occupation;
    _phoneTextField.text = newContact.phone;
    _emailTextField.text = newContact.email;
    _wechatTextField.text = newContact.wechat;

    
}
//删除
- (IBAction)deleteButtonClicked:(id)sender {
    if (_index >= _contactsArray.count){
        _index = _contactsArray.count - 1;
    }
    if (_index<0) {
        return;
    }
    Contact *newContact = [_contactsArray objectAtIndex:_index];
    _nameTextField.text = newContact.name;
    _genderTextField.text = newContact.gender;;
    _companyTextField.text = newContact.company;
    _occupationTextField.text = newContact.occupation;
    _phoneTextField.text = newContact.phone;
    _emailTextField.text = newContact.email;
    _wechatTextField.text = newContact.wechat;

    [_contactsArray removeObject:newContact];
    [self saveContacts];
    if (_contactsArray == 0) {
        return;
    }
    _nameTextField.text = nil;
    _genderTextField.text = nil;
    _companyTextField.text = nil;
    _occupationTextField.text = nil;
    _phoneTextField.text = nil;
    _emailTextField.text = nil;
    _wechatTextField.text = nil;

    [self saveContacts];
}
//搜索
- (IBAction)searchBtnClicked:(id)sender {
    NSString *input = _searchTextField.text;
    if (input && input.length>0) {
        [self searchWithPredicateString:input];
    }
    [self.view endEditing:YES];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

#pragma mark 搜索
- (void)searchWithPredicateString:(NSString *)input{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:input];
    NSArray *resultArray = [_contactsArray filteredArrayUsingPredicate:predicate];
    NSMutableString *text = [NSMutableString string];
    for (Contact *contact in resultArray) {
        NSString *contactStr = [NSString stringWithFormat:@"%@|%@|%@|%@|%@|%@|%@\n",contact.name,contact.gender,contact.company,contact.occupation,contact.phone,contact.email,contact.wechat];
        [text appendString:contactStr];
    }
    _resultTextView.text = text;
}

#pragma mark 加载联系人
- (void)loadContacts{
    //从保存的
    NSMutableDictionary *allContactDic = [NSMutableDictionary dictionaryWithContentsOfFile:_fileSavePath];
    if (!allContactDic)
        return;
    for (NSString *key in [allContactDic allKeys]) {
        NSMutableDictionary *tmpDic = [allContactDic objectForKey:key];
        Contact *contact = [Contact new];
        contact.name = [tmpDic objectForKey:@"name"];
        contact.gender = [tmpDic objectForKey:@"gender"];
        contact.company = [tmpDic objectForKey:@"company"];
        contact.occupation = [tmpDic objectForKey:@"occupation"];
        contact.phone = [tmpDic objectForKey:@"phone"];
        contact.email = [tmpDic objectForKey:@"email"];
        contact.wechat = [tmpDic objectForKey:@"wechat"];
        [_contactsArray addObject:contact];
    }
    if (_contactsArray.count>0) {
        [self showInfo:[_contactsArray firstObject]];
    }
}
-(void)showInfo:(Contact *)contact{
    _nameTextField.text = contact.name;
    _genderTextField.text = contact.gender;
}

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self getPrepared];
    [self loadContacts];
    // [self displayLayer:[_contactsArray firstObject]];
    
}
#pragma mark 保存
- (void)saveContacts{
    NSMutableDictionary *allContactsDic = [NSMutableDictionary dictionary];
    for (Contact *tmp in _contactsArray) {
        NSMutableDictionary *singleContact = [NSMutableDictionary dictionary];
        [singleContact setObject:tmp.name forKey:@"name"];
        [singleContact setObject:tmp.gender forKey:@"gender"];
        [singleContact setObject:tmp.company forKey:@"company"];
        [singleContact setObject:tmp.occupation forKey:@"occupation"];
        [singleContact setObject:tmp.phone forKey:@"phone"];
        [singleContact setObject:tmp.email forKey:@"email"];
        [singleContact setObject:tmp.wechat forKey:@"wechat"];
        
        [allContactsDic setObject:singleContact forKey:tmp.name];
        [allContactsDic setObject:singleContact forKey:tmp.gender];
        [allContactsDic setObject:singleContact forKey:tmp.company];
        [allContactsDic setObject:singleContact forKey:tmp.occupation];
        [allContactsDic setObject:singleContact forKey:tmp.phone];
        [allContactsDic setObject:singleContact forKey:tmp.email];
        [allContactsDic setObject:singleContact forKey:tmp.wechat];
    }
    [allContactsDic writeToFile:_fileSavePath atomically:YES];
}

Contact类文件

Contact.h

#import <Foundation/Foundation.h>

@interface Contact : NSObject

@property (nonatomic,strong) NSString *name;
@property (nonatomic,strong) NSString *gender;
@property (nonatomic,strong) NSString *company;
@property (nonatomic,strong) NSString *occupation;
@property (nonatomic,strong) NSString *phone;
@property (nonatomic,strong) NSString *email;
@property (nonatomic,strong) NSString *wechat;

- (instancetype)initName:(NSString *)name andGender:(NSString *)gender andCompany:(NSString *)company andOccupation:(NSString *)occupation andPhone:(NSString *)phone andEmail:(NSString *)email andWechat:(NSString *)wechat;

@end

Contact.m

#import "Contact.h"

@implementation Contact
//初始化
- (instancetype)initName:(NSString *)name andGender:(NSString *)gender andCompany:(NSString *)company andOccupation:(NSString *)occupation andPhone:(NSString *)phone andEmail:(NSString *)email andWechat:(NSString *)wechat{
    if (self = [super init]) {
        _name = name;
        _gender = gender;
        _company = company;
        _occupation = occupation;
        _phone = phone;
        _email = email;
        _wechat = wechat;
    }
    return self;
}

@end


你可能感兴趣的:(谓词)