IOS project-Address Book, ios Project address book

Source: Internet
Author: User

IOS project-Address Book, ios Project address book

This is mainly for the geek College's address book project, knowledge point sorting... click-> download code

1. When the login and password input boxes are empty, they are unavailable.

Implementation solution: add an observer to the viewDidLoad event to listen to events that change the content of the nameField and pwdField text boxes. After the content changes, the textChange event of the current class is triggered.

-(Void) viewDidLoad {[super viewDidLoad]; // Add the observer [[nsicationicationcenter defaultCenter] addObserver: self selector: @ selector (textChange) name: UITextFieldTextDidChangeNotification object: self. nameField]; [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (textChange) name: UITextFieldTextDidChangeNotification object: self. pwdField];}

---> TextChange Method

-(Void) textChange {// modify the button's click status self. loginBtn. enabled = (self. nameField. text. length & self. pwdField. text. length );}

 

2. Switch between pages

Since storyboard came out, Apple has always recommended storyboard for project implementation. Therefore, the prepareForSegue method has always existed in every view controller.

-(Void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender

This method means preparations before jump. The parameter is UIStoryboardSegue. UIStoryboardSegue has three attributes: 1: identifier (identifier), 2: sourceViewController (original controller), 3: destinationViewController)

// Log on to-(IBAction) loginAction {// after clicking it, execute the jump. logtailcontact is the ID of the seue In the storyboard [self defined mseguewithidentifier: @ "logtailcontact" sender: nil];}

Call the prepareForSegue method before redirecting

/* Generally, the sender is the sender passed in by the initialize mseguewithidentifier method. */-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {// 1. obtain the target controller UIViewController * contactVc = segue. destinationViewController; // 2. set the title (pass value) contactVc. title = [NSString stringWithFormat: @ "% @ Contact List", self. nameField. text];}

 

3. Use a third-party control MBProgressHUD to implement the progress prompt.

Before using the header file # import "MBProgressHUD + MJ. h", MBProgressHUD + MJ. h is a further encapsulation of MBProgressHUD, in order to facilitate the use of this + MJ.

// Log on to-(IBAction) loginAction {if (! [Self. nameField. text isw.tostring: @ "jike"]) {[MBProgressHUD showError: @ "account does not exist"]; return;} if (! [Self. pwdField. text isprogretostring: @ "qq"]) {[MBProgressHUD showError: @ "Password error"]; return ;}// display mask (covered) [MBProgressHUD showMessage: @ "loading hard"]; // simulate a 2-second jump. In the future, the network request dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (2 * NSEC_PER_SEC), interval (), ^ {// remove and mask [MBProgressHUD hideHUD]; [self defined mseguewithidentifier: @ "logtailcontact" sender: nil];});}

 

4. Use UIAlertController

After IOS8.0, UIActionSheet and UIAlertView are replaced by UIAlertController.

-(IBAction) backAction :( id) sender {// initialize UIAlertController * alert = [UIAlertController alertControllerWithTitle: @ "? "Message: @" Do you really want to log out? "preferredStyle: Logout]; // Add the button [alert addAction: [UIAlertAction actionWithTitle: @" cancel "style: UIAlertActionStyleCancel handler: nil]; [alert addAction: [UIAlertAction actionWithTitle: @ "OK" style: UIAlertActionStyleDestructive handler: ^ (UIAlertAction * action) {[self. navigationController popViewControllerAnimated: YES];}]; // [self presentViewController: alert animated: YES completion: NULL];}

 

5. add page values

Key points to be implemented are: a. When the add page is displayed, a keypad is displayed, and the cursor is positioned in the input box. For better user experience

-(Void) viewDidAppear :( BOOL) animated {[super viewDidAppear: animated]; // make the name text box the first responder (the keyboard) [self. nameField becomeFirstResponder];}

 

B. Create a Model folder as the Model layer, and add the Model for passing values to the folder.

#import <Foundation/Foundation.h>@interface JKContactModel : NSObject@property (nonatomic, copy) NSString *name;@property (nonatomic, copy) NSString *phone;@end

 

C. Define the AddViewControllerDelegate interface. There is a method in the interface that needs to be passed into AddViewController and JKContactModel.
@ Class prevents mutual Import
@ Property: assign type. To prevent circular references

#import <UIKit/UIKit.h>@class AddViewController,JKContactModel;@protocol AddViewControllerDelegate <NSObject>@optional- (void)addContact:(AddViewController *)addVc didAddContact:(JKContactModel *)contact;@end@interface AddViewController : UIViewController@property (nonatomic, assign) id<AddViewControllerDelegate> delegate;@end

 

D. Write the AddAction Add button under AddViewController and click the event.

RespondsToSelector determines whether an object has implemented a method.

// Add data-(IBAction) AddAction {// 1. disable the current view controller [self. navigationController popViewControllerAnimated: YES]; // pass the value by proxy // if its proxy object responds to our protocol method, it will pass the value if ([self. delegate respondsToSelector: @ selector (addContact: didAddContact :)]) {JKContactModel * contactModel = [[JKContactModel alloc] init]; contactModel. name = self. nameField. text; contactModel. phone = self. phoneField. text; [self. delegate addContact: self didAddContact: contactModel] ;}}

 

E. Finally, return to the contact list controller ContactTableViewController to return to the returned value.

# Pragma mark-AddViewController delagate-(void) addContact :( AddViewController *) addVc didAddContact :( JKContactModel *) contact {// 1. add data model [self. contactArr addObject: contact]; // 2. refresh table view [self. tableView reloadData];}

 

F. You need to set the proxy as yourself in the prepareForSegue method.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {     AddViewController *addVc = segue.destinationViewController;    addVc.delegate = self;}

 

6. Modify the page Value
This page is basically the same as the above pages... there is no knowledge to organize...
There is still a problem. The ContactTableViewController controller has two target controllers, so the target controller of segeue is needed to determine where to jump.
Modify the prepareForSegue Method

-(Void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {id vc = segue. destinationViewController; if ([vc isKindOfClass: [AddViewController class]) {// if you are redirected to add a contact controller // set the proxy AddViewController * addVc = vc; addVc. delegate = self;} else if ([vc isKindOfClass: [EditViewController class]) {EditViewController * editVc = vc; // obtain the selected NSIndexPath * path = [self. tableView indexPathForSelectedRow]; editVc. contactModel = self. contactArr [path. row]; editVc. delagate = self ;}}

 

7. UITableView implements sliding Deletion
UITableView has a method called commitEditingStyle, which enables deletion.

# Pragma mark-UITableView delagate-(void) tableView :( UITableView *) tableView commitEditingStyle :( partial) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath {if (editingStyle = inline) {/1. delete data model [self. contactArr removeObjectAtIndex: indexPath. row]; // 2. refresh table view [self. tableView deleteRowsAtIndexPaths: @ [indexPath] withRowAnimation: UITableViewRowAnimationTop];}

 

8. preference settings
The main points to be implemented are: a. Load the last configuration in the viewDidLoad event

-(Void) viewDidLoad {[super viewDidLoad]; // read the last configuration NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; self. nameField. text = [defaults valueForKey: UserNameKey]; self. pwdField. text = [defaults valueForKey: PwdKey]; self. rembSwitch. on = [defaults boolForKey: RmbPwdKey]; if (self. rembSwitch. isOn) {self. pwdField. text = [defaults valueForKey: PwdKey]; self. loginBtn. enabled = YES ;}}


B. Save the configuration after the user logs in successfully.

// Login-(IBAction) loginAction {// store data NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject: self. nameField. text forKey: UserNameKey]; [defaults setObject: self. pwdField. text forKey: PwdKey]; [defaults setBool: self. rembSwitch. isOn forKey: RmbPwdKey]; // sets synchronization [defaults synchronize];}

 

9. Object Archiving
Key points to be implemented are: a. NSKeyedArchiver must implement the NSCoding protocol method.

# Import "JKContactModel. h "@ implementation JKContactModel/* when writing an object to a file, it will call this method to clarify which attributes need to be stored */-(void) encodeWithCoder :( NSCoder *) encoder {[encoder encodeObject: self. name forKey: @ "name"]; [encoder encodeObject: self. phone forKey: @ "phone"];}/* attributes that need to be parsed when the method is called */-(id) initWithCoder :( NSCoder *) decoder {if (self = [super init]) {self. name = [decoder decodeObjectForKey: @ "name"]; self. phone = [decoder decodeObjectForKey: @ "phone"];} return self ;}@ end


B. Create an archive file name and load the archive file to the memory when it is used.

# Define ContactFilePath [[NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: @ "contacts. data "] @ property (nonatomic, strong) NSMutableArray * contactArr; @ implementation ContactTableViewController-(NSMutableArray *) contactArr {if (! _ ContactArr) {// read from the archive object first. if no, create an array _ contactArr = [NSKeyedUnarchiver unarchiveObjectWithFile: ContactFilePath]; if (_ contactArr = nil) {_ contactArr = [NSMutableArray array] ;}} return _ contactArr ;}

 

C. Data must be archived after data is added, updated, or deleted.

[NSKeyedArchiver archiveRootObject:self.contactArr toFile:ContactFilePath];

 

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.