2016-1-5 the implementation of the first full app private address Book 4: Edit Contacts

Source: Internet
Author: User
Tags uikit

One: Establish a controller for editing contacts and have the model contact, and there is a protocol. The code is as follows

#import<UIKit/UIKit.h>#import "contact.h"@classEditcontactviewcontroller;@protocoleditcontactviewcontrollerdelegate<nsobject>-(void) Editcontactviewcontroller: (Editcontactviewcontroller *) Editcontactviewcontroller finishedSaveContact: (Contact *) con;@end@interfaceEditcontactviewcontroller:uiviewcontroller@property (Strong, nonatomic) Contact*Contact : @property (weak, nonatomic) Iboutlet Uitextfield*NameField, @property (weak, nonatomic) Iboutlet Uitextfield*Telfield, @property (weak, nonatomic)ID<EditContactViewControllerDelegate>Delegate;@end

The code in the. m file is as follows:

#import "EditContactViewController.h"@interfaceEditcontactviewcontroller () @property (weak, nonatomic) Iboutlet UIButton*savebtn, @property (weak, nonatomic) Iboutlet Uibarbuttonitem*editbtn;@end@implementationEditcontactviewcontroller- (void) viewdidload {[Super viewdidload]; Self.nameField.text=Self.contact.name; Self.telField.text=Self.contact.tel;}-(Ibaction) Savebtnclick: (ID) Sender {if([Self.Delegaterespondstoselector: @selector (editcontactviewcontroller:finishedsavecontact:)]) {//NSLog (@ "%s", __func__);Self.contact.name =Self.nameField.text; Self.contact.tel=Self.telField.text; [Self.Delegateeditcontactviewcontroller:self FinishedSaveContact:self.contact]; }}-(Ibaction) Editbtnclick: (ID) Sender {self.nameField.enabled= !self.nameField.enabled; Self.telField.enabled= !self.telField.enabled; Self.saveBtn.hidden= !Self.saveBtn.hidden; if(self.nameField.enabled) {Self.editBtn.title=@"Cancel"; }Else{self.editBtn.title=@"Edit"; }}@end

Two:

(1) In Contactscontroller, the data in the cell that is clicked is passed to the editor controller, the code is as follows:

//determine the target controller type    if([DESTVC Iskindofclass:[editcontactviewcontrollerclass]]) {Editcontactviewcontroller*edit =DESTVC; Nsinteger Indexpathofrow=Self.tableView.indexPathForSelectedRow.row;//gets the contact to be passed to the target controllerContact *con =Self.contacts[indexpathofrow];//set the Contact property of the target controllerEdit.contact =con;//set your own proxy for the target controllerEdit.Delegate=Self ; }

(2) Implement the Proxy method in Contactscontroller and refresh the data with the following code:

- (void) Editcontactviewcontroller: (Editcontactviewcontroller *) Editcontactviewcontroller finishedSaveContact: (Contact *) con{//get the contact's data and load it into your contacts//find the incoming contact in your own array and get what lineNsinteger row =[Self.contacts Indexofobject:con];//find the cell that you want to refreshNsindexpath *path = [Nsindexpath indexpathforrow:row insection:0];//Local Refresh[Self.tableview Reloadrowsatindexpaths:@[path] withrowanimation:yes];//Hide Edit Contact Controller[Self.navigationcontroller Popviewcontrolleranimated:yes];}

Three: The actual effect is as follows:

2016-1-5 the implementation of the first full app private address Book 4: Edit 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.