2016-1-5 implementation of the first full app private address Book 3: Add contacts
Last Update:2016-01-06
Source: Internet
Author: User
<span id="Label3"></p><p><p>A: Create a model object: contact for storing data and for easy reading loading</p></p><pre class="brush:objc;gutter:true;"><pre class="brush:objc;gutter:true;">#import <Foundation/Foundation.h> @interface contact:nsobject@property (strong, nonatomic) nsstring *name;@ Property (strong, Nonatomic) nsstring *tel; @end</pre></pre><p><p>Second: set up the proxy protocol in Addcontackviewcontroller and listen to the input field</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span><UIKit/UIKit.h><span style="color: #0000ff;"><span style="color: #0000ff;">@class</span></span><span style="color: #000000;"><span style="color: #000000;">addcontactviewconroller,contact;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@protocol</span></span>Addcontactviewconrollerdelegate<nsobject><span style="color: #000000;"><span style="color: #000000;">@optional</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) addcontactviewcontroller: (addcontactviewconroller *) addcontactviewcontroller didsavecontactwithname: (NSString*) Name Tel: (nsstring *<span style="color: #000000;"><span style="color: #000000;">) tel;</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">delivery model makes code maintainability higher</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) addcontactviewcontroller: (addcontactviewconroller *) addcontactviewcontroller didsavecontactwithcontact: (contact *<span style="color: #000000;"><span style="color: #000000;">) contact;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@interface</span></span><span style="color: #000000;"><span style="color: #000000;">addcontactviewconroller:uiviewcontroller@property (weak, Nonatomic)</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span><AddContactViewConrollerDelegate><span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span><span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span></pre><p><p>The code in The. m file is as Follows:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span> <span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">AddContactViewConroller.h</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span> <span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">contact.h</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@interface</span></span><span style="color: #000000;"><span style="color: #000000;">addcontactviewconroller () @property (weak, nonatomic) iboutlet Uitextfield</span></span>*<span style="color: #000000;"><span style="color: #000000;">nameField, @property (weak, nonatomic) iboutlet Uitextfield</span></span>*<span style="color: #000000;"><span style="color: #000000;">telfield, @property (weak, nonatomic) iboutlet UIButton</span></span>*<span style="color: #000000;"><span style="color: #000000;">savebtn;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@implementation</span></span><span style="color: #000000;"><span style="color: #000000;">Addcontactviewconroller</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) viewdidload {[super viewdidload];}</span></span>-<span style="color: #000000;"><span style="color: #000000;">(ibaction) Savebtnclick {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">determine if the agent implements the proxy method</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">if ([self.delegate respondstoselector: @selector (addContactViewController:didSaveContactwithName:tel:)]) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">[self.delegate addcontactviewcontroller:self DidSaveContactwithName:self.nameField.text tel:self.telField.te xt];</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>([self.<span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span><span style="color: #000000;"><span style="color: #000000;">respondstoselector: @selector (addcontactviewcontroller:didsavecontactwithcontact:)]) {</span> contact</span>*con =<span style="color: #000000;"><span style="color: #000000;">[contact alloc] init]; Con.name</span></span>=<span style="color: #000000;"><span style="color: #000000;">self.nameField.text; Con.tel</span></span>=<span style="color: #000000;"><span style="color: #000000;">self.telField.text;</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Direct-to-proxy Model Data</span></span>[self.<span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span><span style="color: #000000;"><span style="color: #000000;">addcontactviewcontroller:self didsavecontactwithcontact:con]; }}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span></pre><p><p>Three:</p></p><p><p>1) create an array in Contacttableviewcontroller to hold the accepted data to the model, with the following code:</p></p><pre><pre><span style="color: #0000ff;">#import</span> <span style="color: #800000;">"</span> <span style="color: #800000;">ContactTableViewController.h</span> <span style="color: #800000;">"</span> <span style="color: #0000ff;">#import</span> <span style="color: #800000;">"</span> <span style="color: #800000;">contact.h</span> <span style="color: #800000;">"</span> <span style="color: #0000ff;">#import</span> <span style="color: #800000;">"</span> <span style="color: #800000;">AddContactViewConroller.h</span> <span style="color: #800000;">"</span> <span style="color: #0000ff;">@interface</span> contacttableviewcontroller () <AddContactViewConrollerDelegate><span style="color: #008000;">//</span> <span style="color: #008000;"> Defines a mutable array to hold the contact</span> @property (strong, nonatomic) nsmutablearray *<span style="color: #000000;">contacts;</span> <span style="color: #0000ff;">@end</span></pre></pre><p><p>2) Implement the data source method of the controller, and load the data from the model to display on the cell</p></p><p><p>The code is as Follows:</p></p><pre>-(nsinteger) Numberofsectionsintableview: (uitableview *<span style="color: #000000;"><span style="color: #000000;">) TableView {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">;}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">number of rows per group (row)</span></span>-(nsinteger) TableView: (uitableview *<span style="color: #000000;"><span style="color: #000000;">) TableView numberofrowsinsection: (nsinteger) section {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">self.contacts.count;}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Setting display content</span></span>-(uitableviewcell *) tableView: (uitableview *) tableView cellforrowatindexpath: (nsindexpath *<span style="color: #000000;"><span style="color: #000000;">) Indexpath{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%s</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, __func__);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">you need to set the cell ID first to create a reusable Cell. </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1. Get a reusable ID</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span>NSString *id =<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Contactcell</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">2. Create a reusable cell</span></span>UITableViewCell *cell =<span style="color: #000000;"><span style="color: #000000;">[tableView dequeuereusablecellwithidentifier:id]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">3. Display Data</span></span>Contact *contact =<span style="color: #000000;"><span style="color: #000000;">self.contacts[indexpath.row]; Cell.textLabel.text</span></span>=<span style="color: #000000;"><span style="color: #000000;">contact.name; Cell.detailTextLabel.text</span></span>=<span style="color: #000000;"><span style="color: #000000;">contact.tel; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">4. Return to cell</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">cell;}</span></span></pre><p><p>3) set yourself as the agent for Addcontactviewcontroller and implement the proxy method, the code is as Follows:</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">-(void) addcontactviewcontroller: (addcontactviewconroller *) addcontactviewcontroller didsavecontactwithname: ( NSString *) Name Tel: (nsstring *) Tel</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">{</span></span><span style="color: #808080;"><span style="color: #808080;">///</span></span><span style="color: #008000;"><span style="color: #008000;">/build models and assign values</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">contact <span style="color: #008000;">*con = [[contact alloc] init];</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">con.name = name;</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Con.tel = tel;</span></span><span style="color: #808080;"><span style="color: #808080;">///</span></span><span style="color: #008000;"><span style="color: #008000;">/put the model in an array</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">[self.contacts addobject:con];</span></span><span style="color: #808080;"><span style="color: #808080;">///</span></span><span style="color: #008000;"><span style="color: #008000;">/hide Target Controller</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">#warning Although this method is called by the proxy, but the target controller is hidden!</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">[self.navigationcontroller popviewcontrolleranimated:yes];</span></span><span style="color: #808080;"><span style="color: #808080;">///</span></span><span style="color: #008000;"><span style="color: #008000;">/refresh TableView</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">[self.tableview reloaddata];</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) addcontactviewcontroller: (addcontactviewconroller *) addcontactviewcontroller didsavecontactwithcontact: (contact *<span style="color: #000000;"><span style="color: #000000;">) contact{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">put the model in an array</span></span><span style="color: #000000;"><span style="color: #000000;">[self.contacts addobject:contact]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Refresh TableView</span></span>Nsindexpath *indexpath = [nsindexpath indexpathforrow:self.contacts.count-<span style="color: #800080;"><span style="color: #800080;">1</span></span>Insection:<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">]; [self.tableview insertrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationleft];</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">[self.tableview reloaddata]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Hide the target controller</span></span><span style="color: #000000;"><span style="color: #000000;">[self.navigationcontroller popviewcontrolleranimated:yes];}</span></span></pre><p><p>4) obtain the data from the target controller as Follows:</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get the target controller</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) prepareforsegue: (uistoryboardsegue *) Segue Sender: (<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span><span style="color: #000000;"><span style="color: #000000;">) sender{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span>DESTVC =<span style="color: #000000;"><span style="color: #000000;">segue.destinationviewcontroller;</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">determine the type of the target controller</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>([DESTVC Iskindofclass:[addcontactviewconroller<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">]]) {addcontactviewconroller</span></span>*addcontactviewcontroller =<span style="color: #000000;"><span style="color: #000000;">destvc;</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set the agent for target control if the type of the target controller is met</span></span>Addcontactviewcontroller.<span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span>=<span style="color: #000000;">self <span style="color: #000000;">; }}</span></span></pre><p><p>Four: the actual effect is as Follows:</p></p><p><p></p></p><p><p>2016-1-5 implementation of the first full app private address Book 3: Add contacts</p></p></span>