One, Project introduction
First, briefly describe the effect of this project
Enter the program, the first is the login page
Login page Use Nsuserdefault remember login password
Then the
Then the login jump, use the Mbprogresshud
Then the contact list is a uitableview.
Click + to go to the Add Contact page
Click on each line of contact to enter the edit screen.
Click Logout to Eject Uialertcontroller
Two, enter the code
First build the schema of the view in SB
Then set up several corresponding controllers, and do a good job of sub-category link
Then implement the Login interface
First add the observer to monitor the change of the value of the 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];
Set up
Nsuserdefaults
Remember password
Setting events
-(void) TextChange
{
self.loginBtn.enabled = self.nametxtfiled.text.length&&self.pwdtextfield.text.length;
}
Set a jump for each Segwe
-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {
if ([Segue.identifier isequaltostring:@ "id"]) {
Contacttableviewcontroller *CONTACTVC = Segue.destinationviewcontroller;
Contactvc.title = [NSString stringwithformat:@ "%@ contact list", Self.nameTxtFiled.text];
}
}
Click events
-(ibaction) Loginclick {
if (![ Self.nameTxtFiled.text isequaltostring:@ "DSN"]) {
[Mbprogresshud showerror:@ "User name error"];
Return
}
if (![ Self.pwdTextField.text isequaltostring:@ "123"])
{
[Mbprogresshud showerror:@ "password error"];
Return
}
[Mbprogresshud showmessage:@ "is landing, please wait ..."];
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 the can be recreated.
}
/*
#pragma mark-navigation
In a storyboard-based application, you'll 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 the 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:@ "%@ contact list", Self.nameTxtFiled.text];
}
}
-(ibaction) Loginclick {
if (![ Self.nameTxtFiled.text isequaltostring:@ "DSN"]) {
[Mbprogresshud showerror:@ "User name error"];
Return
}
if (![ Self.pwdTextField.text isequaltostring:@ "123"])
{
[Mbprogresshud showerror:@ "password error"];
Return
}
[Mbprogresshud showmessage:@ "is landing, please wait ..."];
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 the can be recreated.
}
/*
#pragma mark-navigation
In a storyboard-based application, you'll 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 = @ "edit";
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 = @ "Cancel";
}
}
@end
GitHub Address https://github.com/disneydusunan/10--demo-
iOS Project-Contact list