"iOS Development-76" Private Contacts case: Navigation controller usage, data transfer, third-party class library use, Tableviewcell additions and deletions, data storage, etc.

Source: Internet
Author: User

(1) Effect

(2) Source code and third-party class library download


(3) Summary

--Navigation controller, you can directly use the code push and pop to control the jump between controllers. You can also use storyboard's segue to: There are 2, one is directly dragged to another controller with a button to form a segue, this segue cannot be intercepted, if you click directly jump. The other is from a controller dragged to another controller formed by the segue, this segue does not have a clear click on who to jump, so there is a performseguewithidentifier method, an implementation of this method jumps, so generally used in the need to judge the jump above, such as "Login" button, after adding Addtarget, in the button click Method to perform perform jump.

--The most important thing is the transfer of data, the method of forward transfer, mainly using the properties of Segue Destinationviewcontroller to obtain the latter controller, and then assign a value to the back of this controller, can be assigned to a place, A data model object can also be used to assign the entire data model to the controller behind it, provided that the data model properties are available for all two controllers. And this assignment is generally done in prepareforsegue, because this is a method that is called before Segue jumps.

--there is a reverse pass, that is, when you click "Back", the value needs to be passed from the current page to the page after "back". You need to use the proxy, which is to set up a protocol on the current page, and then set a proxy property and proxy method (which is used primarily to pass data), and the parameters of this proxy method can be a value or a data model object. Then on the "Back" page, adhere to the Protocol and implement proxy methods, receive data, and then data processing.

--we have useful here to tableview, so it involves adding and removing cells. We also refer to the data permanent storage, in this case we use the Nskeyedarchive method to store the data in the database format. So every time we delete and add data, we need not only to "refresh" the TableView, but also to archive the data again.

--When we lazy load the array, we also have one more judgment: If the array is empty, we will read the data from the data file with Nskeyedunarchive, and then determine if the data is read or empty initialization of the data created. Both the nskeyedunchive and the above nskeyedarchive need data model objects to comply with the Nscoding protocol and implement the Init and encode methods, that is, the way to archive and archive data, if there are subclasses, only need to inherit the parent class, Then implement the subclasses themselves.

--The use of third-party libraries, you can drag the. m and. h files directly into the project, of course, there are other installation methods. Here we use Mbprogresshud, download and use the method see: Https://github.com/jdg/MBProgressHUD

-This is the change of the value of the listening keyboard, we use the notification, remember: When using the notification, you must remove the notification in the DEALLOC function, develop good habits:

-(void) viewdidload {    [[nsnotificationcenter defaultcenter]addobserver:self selector: @selector (changetext) name : Uitextfieldtextdidchangenotification Object:self.userField];    [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (changetext) Name: Uitextfieldtextdidchangenotification Object:self.pwdField];    [Super Viewdidload];    Do any additional setup after loading the view.} -(void) dealloc{    [[Nsnotificationcenter defaultcenter]removeobserver:self];}

--the right side of the navigation bar in storyboard can not add 2 items, so it can only be implemented in code, that is, the right one is taken out first, then create a, and finally the 2 set of values to the item.

In storyboard can not add two right button, only code    Uibarbuttonitem *additem=self.navigationitem.rightbarbuttonitem;    Uibarbuttonitem *deleteitem=[[uibarbuttonitem alloc]initwithtitle:@ "operation" Style:uibarbuttonitemstyleplain target: Self action: @selector (Deleteclick)];    [Email protected] [Additem,deleteitem];

--tableview has an edit operation, that is, an entry into the editing operation, similar to the "Text deletion interface" effect, on the left side of all cells a "minus" delete button. The default is delete, but you can modify it to add a style. The following code is the implementation of TableView click to edit and then click the non-editable effect.

-(void) deleteclick{    self.tableview.editing=!self.tableview.isediting;}

--The following method is to set the left side of each cell is the deletion of "minus", or "plus" added. Our line is to add a row that is deleted.

-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) indexpath{    return indexpath.row%2? Uitableviewcelleditingstyledelete:uitableviewcelleditingstyleinsert;}

--Click the logout button after the Uiactionsheet, and do not use Uialert, and in order to listen to the click on the sheet on which button, the controller also adhere to the Protocol uiactionsheetdelegate, and realize the judgment: if you click "OK" , then exit and return to the login interface, which means you need to implement pop operations on the navigation controller.

-(Ibaction) Logoutclick: (ID) Sender {    uiactionsheet *sheet=[[uiactionsheet alloc]initwithtitle:@ "OK to sign out?" "Delegate:self cancelbuttontitle:@" Cancel "destructivebuttontitle:@" OK "otherbuttontitles:nil, nil];    [Sheet ShowInView:self.view];}

-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (nsinteger) buttonindex{    if (buttonIndex= =0) {        [Self.navigationcontroller popviewcontrolleranimated:yes];    }}

--In the TableView controller, because there are 2 kinds of jumps, so you need to iskindofclass to determine which page to jump to. This inside the else if is to achieve a positive transfer of data, directly assigned to the value.
-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{    ID vc=segue.destinationviewcontroller;    if ([VC Iskindofclass:[wpaddviewcontroller Class]]) {        Wpaddviewcontroller *addvc=vc;        addvc.delegate=self;    } else if ([VC Iskindofclass:[wpeditviewcontroller Class]]) {        Wpeditviewcontroller *editvc= Segue.destinationviewcontroller;        Nsindexpath *path=[self.tableview Indexpathforselectedrow];        Editvc.contact=self.contacts[path.row];        editvc.delegate=self;}    }

--Whenever a "minus" or "plus" appears to the left of the cell, we'll call the following method as soon as we click. So here we can determine exactly what button clicked, and then do the action, if the minus sign Delete, we delete the data, update the table, archive, if it is to add data, we also add data, update the table, archive. However, instead of updating all cells in the table with reload, our update table here uses the TableView Delete and insert methods to implement the partial update.

Delete data or add data-(void) TableView: (UITableView *) TableView Commiteditingstyle: (Uitableviewcelleditingstyle)        Editingstyle Forrowatindexpath: (Nsindexpath *) indexpath{if (editingstyle==uitableviewcelleditingstyledelete) {        1, first update the model [self.contacts RemoveObjectAtIndex:indexPath.row]; 2, and then refresh the table (we recommend using the first only to refresh the cells below the deletion row, the second to refresh all tables, consumption performance) [Self.tableview Deleterowsatindexpaths:@[indexpath]        Withrowanimation:uitableviewrowanimationtop];        [Self.tableview Reloaddata];    [Nskeyedarchiver archiveRootObject:self.contacts Tofile:filepath];        }else if (editingstyle==uitableviewcelleditingstyleinsert) {wpcontact *p2=[[wpcontact alloc]init];        [email protected] "Jack";        [email protected] "10086";        [Self.contacts insertobject:p2 atindex:indexpath.row+1];        Nsindexpath *indexpathp2=[nsindexpath indexpathforrow:indexpath.row+1 insection:0]; [Self.tableview Insertrowsatindexpaths:@[indexpathp2] Withrowanimation:uitableviewrowanimaTionbottom];    [Nskeyedarchiver archiveRootObject:self.contacts Tofile:filepath]; }}

--because data archiving is implemented, each time the data is changed (that is, an array of data is stored here), it needs to be archived again.

"iOS Development-76" Private Contacts case: Navigation controller usage, data transfer, third-party class library use, Tableviewcell additions and deletions, data storage, etc.

Related Article

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.