forked from henkbraber/MyContacts-CoreData
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyDetailViewController.m
69 lines (60 loc) · 2.14 KB
/
MyDetailViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// MyDetailViewController.m
// MyContacts
//
// Created by Charles Konkol on 11/10/13.
// Copyright (c) 2013 Chuck Konkol. All rights reserved.
//
#import "MyDetailViewController.h"
@interface MyDetailViewController ()
@end
@implementation MyDetailViewController
@synthesize contactdb;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
if (self.contactdb) {
[self.fullname setText:[self.contactdb valueForKey:@"fullname"]];
[self.email setText:[self.contactdb valueForKey:@"email"]];
[self.phone setText:[self.contactdb valueForKey:@"phone"]];
}
}
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnSave:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
if (self.contactdb) {
// Update existing device
[self.contactdb setValue:self.fullname.text forKey:@"fullname"];
[self.contactdb setValue:self.email.text forKey:@"email"];
[self.contactdb setValue:self.phone.text forKey:@"phone"];
} else {
// Create a new device
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Contacts" inManagedObjectContext:context];
[newDevice setValue:self.fullname.text forKey:@"fullname"];
[newDevice setValue:self.email.text forKey:@"email"];
[newDevice setValue:self.phone.text forKey:@"phone"];
}
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)btnBack:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
@end