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