Ø (26)Database
Using Plist (add and Edit phone number)
DJ Delegate.h
DJ Delegate.m
DJ ViewController.h
@property (nonatomic, retain) IBOutlet UILabel *nameLbl;
@property (nonatomic, retain) IBOutlet UITextView
*phoneTxtView;
- (IBAction)editButtonPressed:(id)sender;
DJViewController.m
#import "AddRec.h"
@synthesize nameLbl,
phoneTxtView;
- (IBAction)editButtonPressed:(id)sender
{
AddRec *addRecVC =
[[AddRec alloc] init];
[self
presentViewController:addRecVC animated:YES completion:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
[super
viewWillAppear:animated];
NSArray *paths =
NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask,
YES);
NSString
*documentsPath = [paths objectAtIndex:0];
NSString
*plistPath = [documentsPath
stringByAppendingPathComponent:@"Data.plist"];
if
(![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath
= [[NSBundle mainBundle] pathForResource:@"Data"
ofType:@"plist"];
}
NSData
*plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString
*errorDesc = nil;
NSPropertyListFormat
format;
NSDictionary *temp
= (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format errorDescription:&errorDesc];
if (!temp)
{
NSLog(@"Error
reading plist: %@, format: %d", errorDesc, format);
}
NSString
*personName = [temp objectForKey:@"Name"];
NSMutableArray
*phoneNumbers = [NSMutableArray arrayWithArray:[temp
objectForKey:@"Phones"]];
nameLbl.text =
personName;
phoneTxtView.text
= nil;
for (int i = 0 ; i
< phoneNumbers.count ; i++)
{
phoneTxtView.text = [NSString stringWithFormat:@"%@\n%@",
phoneTxtView.text, [phoneNumbers objectAtIndex:i]];
}
}
AddRec.h
{
NSString *personName;
NSMutableArray *phoneNumbers;
}
@property (nonatomic, retain) IBOutlet UITextField *nameEntered,
*homePhone,
*workPhone,
*cellPhone;
@property (nonatomic, retain) IBOutlet UIToolbar *toolBar;
//@property (nonatomic, retain) NSString *personName;
//@property (nonatomic, retain) NSMutableArray
*phoneNumbers;
- (IBAction) saveData;
- (IBAction) textFieldReturn:(id)textField;
AddRec.m
@synthesize nameEntered,
homePhone,
workPhone,
cellPhone,
toolBar;
- (void)viewDidLoad
{
[super
viewDidLoad];
nameEntered.inputAccessoryView = toolBar;
homePhone.inputAccessoryView = toolBar;
workPhone.inputAccessoryView = toolBar;
cellPhone.inputAccessoryView = toolBar;
NSArray *paths =
NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask,
YES);
NSString
*documentsPath = [paths objectAtIndex:0];
NSString
*plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
if
(![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath
= [[NSBundle mainBundle] pathForResource:@"Data"
ofType:@"plist"];
}
NSData
*plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString
*errorDesc = nil;
NSPropertyListFormat
format;
NSDictionary *temp
= (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format
errorDescription:&errorDesc];
if (!temp)
{
NSLog(@"Error
reading plist: %@, format: %d", errorDesc, format);
}
personName = [temp
objectForKey:@"Name"];
phoneNumbers
= [NSMutableArray arrayWithArray:[temp objectForKey:@"Phones"]];
nameEntered.text = personName;
homePhone.text
= [phoneNumbers objectAtIndex:0];
workPhone.text
= [phoneNumbers objectAtIndex:1];
cellPhone.text
= [phoneNumbers objectAtIndex:2];
}
- (IBAction) saveData
{
NSArray
*paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,
NSUserDomainMask, YES);
NSString
*documentsPath = [paths objectAtIndex:0];
NSString
*plistPath = [documentsPath
stringByAppendingPathComponent:@"Data.plist"];
personName
= nameEntered.text;
phoneNumbers
= [[NSMutableArray alloc] initWithCapacity:3];
[phoneNumbers
addObject:homePhone.text];
[phoneNumbers
addObject:workPhone.text];
[phoneNumbers
addObject:cellPhone.text];
NSDictionary
*plistDict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
personName, phoneNumbers, nil]
forKeys:[NSArray arrayWithObjects: @"Name",
@"Phones", nil]];
NSString
*error = nil;
NSData
*plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
if(plistData)
{
[plistData
writeToFile:plistPath atomically:YES];
}
else
{
NSLog(@"Error in saveData: %@", error);
}
[self
dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction) textFieldReturn:(id)sender
{
[nameEntered
resignFirstResponder];
[homePhone
resignFirstResponder];
[cellPhone
resignFirstResponder];
[workPhone
resignFirstResponder];
}