iOS项目-联系人列表
一,项目介绍
首先,简单介绍一下这个项目的效果
进入程序,首先是登录页面
登录页面用到 NSUserDefault 记住登录密码
然后是
然后是登录跳转,用到MBProgressHUD
接着是联系人列表 是一个UITableView
点击+ 进入添加联系人页面
点击每行联系人 进入编辑界面
点击注销,弹出UIAlertController
二,进入代码
首先 在SB中构建视图的架构
然后建立几个对应的控制器,并做好子类链接
然后实现 登录界面
首先添加观察者,监视textfield的值的变化
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.nameTxtFiled];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.pwdTextField];
设置
NSUserDefaults
记住密码
设置事件
-(void)textChange
{
self.loginBtn.enabled = self.nameTxtFiled.text.length&&self.pwdTextField.text.length;
}
设置每一个segwe的跳转
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"id"]) {
ContactTableViewController *contactVC = segue.destinationViewController;
contactVC.title = [NSString stringWithFormat:@"%@的联系人列表",self.nameTxtFiled.text];
}
}
点击事件
- (IBAction)loginClick {
if (![self.nameTxtFiled.text isEqualToString:@"dsn"]) {
[MBProgressHUD showError:@"用户名错误"];
return;
}
if(![self.pwdTextField.text isEqualToString:@"123"])
{
[MBProgressHUD showError:@"密码错误"];
return;
}
[MBProgressHUD showMessage:@"正在登陆,请等待...."];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUD];
[self performSegueWithIdentifier:@"id" sender:nil];
});
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.nameTxtFiled.text forKey:usernameKey];
[defaults setObject:self.pwdTextField.text forKey:userpassKey];
[defaults setBool:self.switchbTN.on forKey:switchKey];
[defaults synchronize];
}
#import "AddViewController.h"
#import "ModelContact.h"
@interface AddViewController ()
@property (weak, nonatomic) IBOutlet UITextField *contactField;
@property (weak, nonatomic) IBOutlet UITextField *numberField;
- (IBAction)addClick:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@end
@implementation AddViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.contactField];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.numberField];
// Do any additional setup after loading the view.
}
-(void)textChange
{
self.addBtn.enabled = self.contactField.text.length && self.numberField.text.length;
}
- (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.
}
*/
- (IBAction)addClick:(UIButton *)sender {
[self.navigationController popViewControllerAnimated:YES];
if ([self.delegate respondsToSelector:@selector(addContactWithAddViewController:addContact:)]) {
ModelContact *contact = [[ModelContact alloc]init];
contact.contact = self.contactField.text;
contact.number = self.numberField.text;
[self.delegate addContactWithAddViewController:self addContact:contact];
}
}
#import "LoginViewController.h"
#import "ContactTableViewController.h"
#import "MBProgressHUD+MJ.h"
#define usernameKey @"name"
#define userpassKey @"pass"
#define switchKey @"switchKey"
@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTxtFiled;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UISwitch *switchbTN;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
- (IBAction)loginClick;
@end
@implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.nameTxtFiled];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.pwdTextField];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.nameTxtFiled.text = [defaults valueForKey:usernameKey];
self.pwdTextField.text = [defaults valueForKey:userpassKey];
self.switchbTN.on = [defaults boolForKey:switchKey];
if (self.switchbTN.isOn) {
self.pwdTextField.text = [defaults valueForKey:userpassKey];
self.loginBtn.enabled = YES;
}
// Do any additional setup after loading the view.
}
-(void)textChange
{
self.loginBtn.enabled = self.nameTxtFiled.text.length&&self.pwdTextField.text.length;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"id"]) {
ContactTableViewController *contactVC = segue.destinationViewController;
contactVC.title = [NSString stringWithFormat:@"%@的联系人列表",self.nameTxtFiled.text];
}
}
- (IBAction)loginClick {
if (![self.nameTxtFiled.text isEqualToString:@"dsn"]) {
[MBProgressHUD showError:@"用户名错误"];
return;
}
if(![self.pwdTextField.text isEqualToString:@"123"])
{
[MBProgressHUD showError:@"密码错误"];
return;
}
[MBProgressHUD showMessage:@"正在登陆,请等待...."];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUD];
[self performSegueWithIdentifier:@"id" sender:nil];
});
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.nameTxtFiled.text forKey:usernameKey];
[defaults setObject:self.pwdTextField.text forKey:userpassKey];
[defaults setBool:self.switchbTN.on forKey:switchKey];
[defaults synchronize];
}
@end
#import "EditViewController.h"
#import "ModelContact.h"
@interface EditViewController ()
@property (weak, nonatomic) IBOutlet UITextField *contactTxtField;
@property (weak, nonatomic) IBOutlet UITextField *numberTxtField;
- (IBAction)saveBtn:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;
- (IBAction)editClick:(UIBarButtonItem *)sender;
@end
@implementation EditViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.contactTxtField];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.numberTxtField];
// Do any additional setup after loading the view.
self.contactTxtField.text = self.contact.contact;
self.numberTxtField.text = self.contact.number;
}
-(void)textChange
{
self.saveBtn.enabled = self.contactTxtField.text.length && self.numberTxtField.text.length;
}
- (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.
}
*/
- (IBAction)saveBtn:(UIButton *)sender {
[self.navigationController popViewControllerAnimated:YES];
if ([self.delegate respondsToSelector:@selector(editContactWithController:editContact:)]) {
self.contact.contact = self.contactTxtField.text;
self.contact.number = self.numberTxtField.text;
[self.delegate editContactWithController:self editContact:self.contact];
}
}
- (IBAction)editClick:(UIBarButtonItem *)sender {
if (self.contactTxtField.enabled) {
self.contactTxtField.enabled = NO;
self.numberTxtField.enabled = NO;
[self.view endEditing:YES];
self.saveBtn.enabled = NO;
sender.title = @"编辑";
self.contactTxtField.text = self.contact.contact;
self.numberTxtField.text = self.contact.number;
}else{
self.contactTxtField.enabled = YES;
self.numberTxtField.enabled = YES;
[self.view endEditing:NO];
self.saveBtn.enabled = YES;
sender.title = @"取消";
}
}
@end
github 地址https://github.com/Disneydusunan/10--DEMO-