iOS Projects-Contacts

Source: Internet
Author: User

Mainly for the Geek College address book items, knowledge points of the collation ... Click to download code

1, the implementation of login and password input box is empty, is not available state

Implementation: Using the method of adding observers, adding observers to the Viewdidload event to listen for events that change the contents of Namefield and Pwdfield text boxes, triggering the TextChange event of the current class after the content has changed

-(void) viewdidload {    [super viewdidload]; // Add observer    Object : Self.namefield];     Object : Self.pwdfield];}

--->textchange method

-(void) textchange{    // Modify the Click state of the button    self.loginBtn.enabled = ( Self.nameField.text.length && self.pwdField.text.length);}

2, to achieve the jump between pages

Since storyboard came out, Apple has always recommended the use of storyboard to implement the project, so prepareforsegue This method has been present in every view controller.

-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender

The meaning of this method is to jump before the preparation work, the parameter is uistoryboardsegue,uistoryboardsegue within three attributes, 1: is identifier (identification), 2:sourceviewcontroller (original controller) , 3:destinationviewcontroller (Target Controller)

// Login - (ibaction) loginaction {    // Click to execute jump, logintocontact for segue in storyboard    [self performseguewithidentifier:@ "logintocontact"  Sender:nil];}

Call the Prepareforsegue method before jumping

/* this is where the sender of the next controller is sent in the original Performseguewithidentifier method. */-(void) Prepareforsegue: (Uistoryboardsegue *) Segue sender: (ID) sender {//  1. Obtain the target controller    uiviewcontroller *CONTACTVC = Segue.destinationviewcontroller    ; // 2. Setting the title (value of the pass)    Contactvc.title = [NSString stringWithFormat:@ "%@ Contact list ", Self.nameField.text];}

3, implementing a progress hint using a third-party control Mbprogresshud

Before using the introduction of the header file #import "Mbprogresshud+mj.h", Mbprogresshud+mj.h is the Mbprogresshud again encapsulation, in order to facilitate the use of this +MJ.

//Login-(ibaction) loginaction {if(! [Self.nameField.text isequaltostring:@"Jike"]) {[Mbprogresshud showerror:@"account does not exist"]; return; }    if(! [Self.pwdField.text isequaltostring:@"QQ"]) {[Mbprogresshud showerror:@"Password Error"]; return; }    //Show Masks (cloaking)[Mbprogresshud ShowMessage:@"try to load in"]; //simulate a 2-second jump to send a network request laterDispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2* nsec_per_sec)), Dispatch_get_main_queue (), ^{        //Remove Cover[Mbprogresshud Hidehud]; [Self performseguewithidentifier:@"logintocontact"Sender:nil]; });}

4, using Uialertcontroller

Uiactionsheet and Uialertview replaced by Uialertcontroller since IOS8.0

-(Ibaction) Backaction: (ID) Sender {//InitializeUialertcontroller *alert = [Uialertcontroller alertcontrollerwithtitle:@"do you want to log off?"Message@"Are you sure you want to sign out?"Preferredstyle:uialertcontrollerstyleactionsheet]; //Add button[Alert addaction:[uialertaction Actionwithtitle:@"Cancel"Style:uialertactionstylecancel Handler:nil]]; [Alert addaction:[uialertaction Actionwithtitle:@"Determine"Style:uialertactionstyledestructive handler:^ (Uialertaction *action)    {[Self.navigationcontroller popviewcontrolleranimated:yes];    }]]; //Popup[self Presentviewcontroller:alert animated:yes completion:null];}

5, implement add page pass value

The main points to be implemented are: A, pop-up the keypad when the page is added, and the cursor is positioned in the input box. To get a better user experience

-(void) viewdidappear: (BOOL) animated{    [Super viewdidappear:animated];     // make a Name text box the first responder (call out the keyboard)     [Self.namefield becomefirstresponder];}

B, create model folder as the model layer, under which you can add models that require a value to be passed

#import <Foundation/Foundation.h>@interface**phone; @end

C, define the interface addviewcontrollerdelegate, there is a method within the interface need to pass in Addviewcontroller and Jkcontactmodel
@class prevent each other from importing
@ The property is of type assign, in order to prevent circular referencing

#import <UIKit/UIKit.h>@class  Addviewcontroller,jkcontactmodel; @protocol Addviewcontrollerdelegate <NSObject>@optional-(void) Addcontact: (Addviewcontroller *) ADDVC didaddcontact: (Jkcontactmodel *) Contact; @end @interface  IDdelegate; @end

D, Write Addaction Add button click event below Addviewcontroller

Respondstoselector determine if an object implements a method

//Add Data-(ibaction) addaction {//1. Close the current View controller[Self.navigationcontroller Popviewcontrolleranimated:yes]; //Proxy Pass -through value//if its proxy object responds to this protocol method, it will pass the value.    if([Self.Delegaterespondstoselector: @selector (addcontact:didaddcontact:)]) {Jkcontactmodel*contactmodel =[[Jkcontactmodel alloc] init]; Contactmodel.name=Self.nameField.text; Contactmodel.phone=Self.phoneField.text; [Self.Delegateaddcontact:self Didaddcontact:contactmodel]; }}

E, finally back to the contact list controller Contacttableviewcontroller to respond to the value

#pragma mark-addviewcontroller delagate-(void) Addcontact: (Addviewcontroller *) ADDVC Didaddcontact: (Jkcontactmodel *) contact{    //1. Adding a data Model     [Self.contactarr Addobject:contact];     // 2. Refresh the table View     [Self.tableview reloaddata];}

F, and one more thing to set the proxy for itself under the Prepareforsegue method

-(void) Prepareforsegue: (Uistoryboardsegue *) Segue sender: (ID) sender {     *ADDVC =  Segue.destinationviewcontroller;    ADDVC. delegate = self ;}

6, implement modify page pass value
This page is basically the same as adding a page above .... There is no knowledge to tidy up ...
This still has a problem, the Contacttableviewcontroller controller has two target controller, this will need to segeue the target controller to determine where to jump to
Modify the Prepareforsegue method

- (void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {IDVC =Segue.destinationviewcontroller; if([VC Iskindofclass:[addviewcontrollerclass]]) {//if it is jump to add contact controller//Set up proxyAddviewcontroller *ADDVC =VC; ADDVC.Delegate=Self ; }Else if([VC Iskindofclass:[editviewcontrollerclass]]) {Editviewcontroller*EDITVC =VC; //get the selected lineNsindexpath *path =[Self.tableview Indexpathforselectedrow]; Editvc.contactmodel=Self.contactarr[path.row]; Editvc.delagate=Self ; }}

7,uitableview for sliding deletion
UITableView there is a method called Commiteditingstyle, through this method to implement the deletion function

#pragma mark-uitableview delagate-(void) TableView: (UITableView *) TableView Commiteditingstyle: ( Uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) indexpath{    if ( Editingstyle = = uitableviewcelleditingstyledelete)        {//1. Delete data Model         [ Self.contactarr RemoveObjectAtIndex:indexPath.row];         // 2. Refresh the table View         [Self.tableview Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationtop];}    }

8, preference settings
The key points to implement are: A, loading the last configuration in the Viewdidload event

-(void) viewdidload {    [super Viewdidload];     // read last configuration    Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];     = [Defaults valueforkey:usernamekey];     = [Defaults valueforkey:pwdkey];     = [Defaults boolforkey:rmbpwdkey];     if (self.rembSwitch.isOn) {        = [Defaults valueforkey:pwdkey];         = YES;    }   }


b, save the configuration after the user login is successful

// Login - (ibaction) loginaction {   // store data    nsuserdefaults *defaults = [ Nsuserdefaults Standarduserdefaults];    [Defaults setObject:self.nameField.text Forkey:usernamekey];    [Defaults setObject:self.pwdField.text Forkey:pwdkey];    [Defaults setBool:self.rembSwitch.isOn Forkey:rmbpwdkey];     // Setting up synchronization     [Defaults synchronize];}

9, Object archiving
The key points to be implemented are: A, nskeyedarchiver must implement the Nscoding protocol approach

#import "JKContactModel.h"@implementationJkcontactmodel/*when writing an object to a file, it is called in this method to clarify which attributes need to be stored*/- (void) Encodewithcoder: (Nscoder *) encoder{[encoder encodeObject:self.name Forkey:@"name"]; [Encoder encodeObject:self.phone Forkey:@"Phone"];}/*the parsing object calls this method to parse which properties*/- (ID) Initwithcoder: (Nscoder *) decoder{if(self =[Super Init]) {Self.name= [Decoder Decodeobjectforkey:@"name"]; Self.phone= [Decoder Decodeobjectforkey:@"Phone"]; }    returnSelf ;}@end


b, then create an archived file name and use it to load the archived file into memory

#defineContactfilepath [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) LastObject] stringbyappendingpathcomponent:@ "Contacts.data"]@property (nonatomic, strong) Nsmutablearray*Contactarr;@implementationContacttableviewcontroller-(Nsmutablearray *) contactarr{if(!_contactarr) {        //read in a file that is first re-archived, and if it does not, create an array_contactarr =[Nskeyedunarchiver Unarchiveobjectwithfile:contactfilepath]; if(_contactarr = =Nil) {_contactarr=[Nsmutablearray array]; }            }    return_contactarr;}

C, the data need to be added, updated, deleted should be archived

[Nskeyedarchiver ARCHIVEROOTOBJECT:SELF.CONTACTARR Tofile:contactfilepath];

iOS Projects-Contacts

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.