[IOS development-76] Private Contacts case: use of navigation controllers, data transmission, use of third-party class libraries, add and delete tableViewCell, data storage, etc., iosprivate

Source: Internet
Author: User

[IOS development-76] Private Contacts case: use of navigation controllers, data transmission, use of third-party class libraries, add and delete tableViewCell, data storage, etc., iosprivate

(1) Effect



(2) download source code and third-party class libraries

Http://download.csdn.net/detail/wsb200514/8155979


(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: Here there are two types. One is to drag a button to another controller to form a segue, which cannot be intercepted. If you click to jump directly. Another method is to drag a stream from one controller to another. This type of Stream does not have a clear idea of who to jump to. Therefore, there is an ingress mseguewithidentifier method that redirects when this method is executed, therefore, it is generally used for jump to be judged, such as the "login" button. After addTarget is added, execute perform jump in the button clicking method.


-- The most important thing is data transmission. The forward transmission method is to use the destinationViewController attribute of segue to obtain the next controller. Then, assign a value to the next controller and assign a value to a certain place, you can also assign the entire data model to the following controllers through a data model object, provided that both controllers have this data model attribute. In addition, this assignment operation is generally performed in prepareForSegue, because this is a method called before the segue jump.


-- There is also reverse transfer, that is, when you click "return", the value needs to be transferred from the current page to the page after "return. Proxy is used here, that is, to set a protocol on the current page, and then set a proxy attribute and proxy method (mainly used to transmit data ), the parameter of this proxy method can be either a value or a data model object. Then, on the page after "return", observe the Protocol and implement the proxy method, receive the data, and then process the data.


-- TableView is useful here. Therefore, it involves adding and deleting cells. We also involve permanent data storage. In this case, we use the NSKeyedArchive method to store data in the data format. Therefore, each time we delete and add data, we not only need to "refresh" tableView, but also archive the data again.


-- When loading arrays lazily, we also need to make another judgment: If the array is empty, we will read data from the data file using NSKeyedUnarchive, and then judge, if the read data is found to be null, initialize and create the data. Both the NSKeyedUnchive and the above NSKeyedArchive require that the data model object comply with the NSCoding Protocol and implement the init and encode methods. This is the method of archiving and archiving data. If there is a subclass here, you only need to inherit the parent class and then implement the sub-class.


-- For third-party class libraries, you can drag. m and. H files directly to the project. Of course, there are other installation methods. Here we use MBProgressHUD, download and use of SEE: https://github.com/jdg/MBProgressHUD


-- We use notifications to listen to changes in the keyboard value. Remember: when using notifications, you must remove the notifications from the dealloc function to develop a good habit:

- (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];}

-- Two items cannot be added to the right of the navigation bar in the storyboard, so they can only be implemented using code, that is, the one on the right is obtained first, and then one is created, finally, assign these two arrays to items.

// You cannot add two buttons on the right in the storyboard. You can only add the code UIBarButtonItem * addItem = self. navigationItem. rightBarButtonItem; UIBarButtonItem * deleteItem = [[UIBarButtonItem alloc] initWithTitle: @ "operation" style: UIBarButtonItemStylePlain target: self action: @ selector (deleteClick)]; self. navigationItem. rightBarButtonItems = @ [addItem, deleteItem];

-- TableView has an editing operation, that is, the editing operation is similar to the "text message deletion interface", and a "minus sign" DELETE button is displayed on the left of all cells. It is deleted by default, but can be modified to add a style. The following code enables tableView to be editable and then click uneditable.

-(void)deleteClick{    self.tableView.editing=!self.tableView.isEditing;}

-- The following method is to set whether to delete the minus sign or add the plus sign on the left side of each cell. Add a row to delete the row.

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    return indexPath.row%2 ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert;}

-- After you click the Logout button, the UIActionSheet button is displayed, instead of UIAlert. To listen to the button on the sheet when you click it, the Controller also complies with the Protocol UIActionSheetDelegate and implements judgment: if you click "OK", exit and return to the logon interface, that is, you need to perform the pop operation on the navigation controller.

-(IBAction) logoutClick :( id) sender {UIActionSheet * sheet = [[UIActionSheet alloc] initWithTitle: @ "are you sure you want to log 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];    }}


-- On the controller of tableView, isKindOfClass is required to determine which page to jump to because there are two types of redirects. Here, else if implements a forward data transfer, which can be directly assigned a 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;    }}

-- When "minus sign" or "plus sign" appears on the left of the cell, the following method is called upon clicking it. So here we can determine what button is clicked, and then perform the operation. If it is a minus sign, we will delete the data, update the table, and archive. If it is to add data, we also add data, update tables, and archive. However, the update table here does not use reload to update all cells in the table, but partial update using the delete and insert methods of tableView.

// Delete data or add data-(void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath {if (editingStyle = response) {// 1. Update the model [self. contacts removeObjectAtIndex: indexPath. row]; // 2. Refresh the table (we recommend that you use the first method to refresh and delete the cells below the row, and the second method to refresh all the tables, consuming 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]; p2.name = @ "jack "; p2.phone = @" 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] ;}}

-- To archive data, you must archive the data every time you change the data (that is, the array where the data is stored.


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.