iOS development-Private contacts-data storage and editing modes

Source: Internet
Author: User

UI interface

-(void) viewdidload
{
[Super Viewdidload];
AddTarget: Commonly used for listening button clicks and progress bar value changes

Monitor the change of Uitextfield by notification
/*
Addobserver: Who's going to listen?
Selector: What method is called when the notification occurs
Name: Notification names
Object: Who sent the notification
Note: Object cannot write nil, because if it is nil, it will call the TextChange method if it is Uitextfield, and we will only call when the account and password input boxes change.
*/
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (TextChange) Name:uitextfieldtextdidchangenotification Object:self.nameField];

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (TextChange) Name:uitextfieldtextdidchangenotification Object:self.pwdField];

Read Preference settings information
Nsuuserdefualts *defualts = [Nsuuserdefualts standarduserdefualts];
Read account information
Self.nameField.text = [Defualts obejctforkey:kaccount];
Do you want to save the password
Self.remPwd.on =[defualts Boolforkey:krempwd];
if (Self.remPwd.on) {//Need to remember password
Self. Pwdfield.text = [Defualts Objectforkey:kpassword];
}
Do I need to log in automatically
Self.autoLogin.on =[defualts Boolforkey:kautologin];
if (Self.autoLogin.on) {//requires automatic login
Manually click the login button
[Self logbtnclick];
}
}
Remove Listener
-(void) dealloc
{...}

/**
* Called when the input box has changed
*/
-(void) TextChange
{...}


#pragma mark-button click event
/**
* Click to remember Password
*/
-(Ibaction) Rempwdswitch
{
Remember the status of the password switch
if (Self.remPwd.on = = NO) {
Cancel the remember password, corresponding cancel automatic login
Self.autoLogin.on = NO;
[Self.autologin Seton:no Animated:yes];
}
}


/**
* Click Automatic Login
*/

-(Ibaction) Autologinswitch
{...}

/**
* Click the login button
* Common framework for iOS development
* Coco4app
*/

-(Ibaction) Logbtnclick
{
Determine if the user name and password are correct
if (self.nameField.text isequaltostring:@ "Lnj") &&
[Self.pwdField.text isequaltostring:@ "123"]) {
Show masks (blocking the user from continuing and prompting the user)
[Mbprogresshud showmessage:@ "is desperately landing ing ..."];

Analog Network login 2 seconds before jumping
Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (0.5 * nsec_per_sec)), Dispatch_get_main_queue (), ^{
Remove a hint box
[Mbprogresshud Hidehud];

//Save preference Information
Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];
//1. Account
[Defaults SetObject:self.nameField.text Forkey:kaccount];
//2. Password
[Defaults setObject:self.pwdField.text Forkey:kpassword];
//3. Automatically login
[defaults setBool:self.autoLogin.on forkey:kautologin];
//4. Remember Password
[ Defaults setBool:self.remPwd.on forkey:@ "Krempwd"];
//Sync Now (save data immediately)
[defaults synchronize];

// Jump to contact list
[self
performseguewithidentifier:@ "login2contacts" sender:@ "Lnj"];
});
   }else
     {
[Mbprogresshud showerror:@ "username or password error"];
     }
}

In the NJCONTACTSVIEWCONTROLLER.M
-(void) Logout
{...}
#pragma mark-uiactionsheetdelegate
Uiactionsheet when the button above is clicked
Buttonindex the index of the clicked button
-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (Nsinteger) Buttonindex
{...}
Segue Jump before calling
-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender
{...}

#pragma mark-njeditviewcontrollerdelegate
-(void) Editviewcontroller: (Njeditviewcontroller *) Editviewcontroller didcontact: (njcontact *) Contact
{
1. Refresh the table
[Self.tableview Reloaddata];

2. Persisting data
[Nskeyedarchiver archiveRootObject:self.contacts Tofile:kfilepath];
}

#pragma mark-njaddviewcontrollerdelegate
-(void) Addviewcontroller: (Njaddviewcontroller *) Addviewcontroller Didcontact: (njcontact *) Contact
{
//1. Save Data
[self.contacts addobject:contact];
//2. Refresh Table
[ Self.tableview Reloaddata];
//3. Persist model data (first to comply with Nscoding protocol and implement protocol method)
[Nskeyedarchiver ArchiveRootObject:self.contacts Tofile:kfilepath];
}
PS: [[ Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes)
LastObject] stringbyappendingpathcomponent:@ "Contacts.arc"];
Define macro Kfilepath

Edit mode (in NJCONTACTSVIEWCONTROLLER.M)
//As long as the method is implemented, the finger is automatically removed when it slides above the cell
// Commiteditingstyle: Edit operation for incoming commit (delete/Add)
//forrowatindexpath: The line currently being edited
-(void) TableView: (UITableView * ) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath:
(Nsindexpath * ) Indexpath
{
//1. Modify the data
[self.contacts RemoveObjectAtIndex:indexPath.row];
//2. Refresh the table
// Reloaddata will recall all methods of the data source. Refresh All Rows
//This method is used to delete cells of the specified row on TableView
//NOTE: When using this method, the number of data bars deleted in the model must be the same as the number of bars removed in the Deleterowsatindexpaths method, or an error will be given
/ /In short, the modified data must be consistent with the deleted cell
[Self.tableview deleterowsatindexpaths:@[indexpath]withrowanimation: Random]
//3. Update saved file
[Nskeyedarchiver archiveRootObject:self.contacts tofile:kcontactspath];
}

#pragma mark-Lazy loading
-(Nsmutablearray *) contacts
{
if (_contacts = = nil) {
Reading data from an archive file
_contacts = [Nskeyedunarchiver Unarchiveobjectwithfile:kfilepath];
if (_contacts = = nil) {//No archived files
_contacts = [Nsmutablearray array];
}
}
return _contacts;
}

iOS development-Private contacts-data storage and editing modes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.