Xcode create database my store

Screen Shot 2556-07-09 at 4.03.19 PM Screen Shot 2556-07-09 at 3.56.27 PM Screen Shot 2556-07-09 at 4.04.43 PM Screen Shot 2556-07-09 at 4.03.37 PMCreate DatabaseScreen Shot 2556-07-09 at 9.47.21 AM

Create View or storyboard

Screen Shot 2556-07-09 at 3.42.51 PM

Write code DeviceTypeViewController.m

//  DeviceTypeViewController.m
//  MyStore
//  Created by mac on 7/9/56 BE.
//  Copyright (c) 2556 Petcharaporn  All rights reserved.
//
#import "DeviceTypeViewController.h"
#import "DetailViewController.h"
@interface DeviceTypeViewController ()
@property (strong) NSMutableArray *devicetypes;
@end
//Every database must be add this code
@implementation DeviceTypeViewController
@synthesize devicetypes;
- (NSManagedObjectContext *)managedObjectContext{
NSManagedObjectContext * context=nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSManagedObjectContext *m = [self managedObjectContext];

NSFetchRequest *fetch = [[NSFetchRequest alloc] initWithEntityName:@"DeviceType"];

self.devicetypes = [[m executeFetchRequest:fetch error: nil] mutableCopy];[self.tableView reloadData];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"UpdateDeviceType"])
{
NSManagedObject *selectDeviceType = [self.devicetypes objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
DetailViewController *destview=segue.destinationViewController;
destview.devicetypes = selectDeviceType;
}
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.devicetypes.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...

NSManagedObject *devicetype = [self.devicetypes objectAtIndex:indexPath.row];

[cell.textLabel setText:[NSString stringWithFormat:@"%@",[devicetype valueForKey:@"name"]]];
return cell;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context=[self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[context deleteObject:[self.devicetypes objectAtIndex:indexPath.row]];[self.devicetypes removeObjectAtIndex:indexPath.row];

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*

// Override to support conditional rearranging of the table view.

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
@end
-------------------------------------

Write Code program DetailViewController.m

//
//  DetailViewController.m
//  MyStore
//
//  Created by mac on 7/9/56 BE.
//  Copyright (c) 2556 Petcharaporn. All rights reserved.
//
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
//This is a connection of XCode
//NSManagedObjectContext is a static class, can run automatic when call this name
- (NSManagedObjectContext *)managedObjectContext{
NSManagedObjectContext * context=nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.devicetypes)
{
[self.deviceName setText:[self.devicetypes valueForKey:@"name"]];
}
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)cancleDeviceType:(id)sender {
//this function have 2 parameter, first para is default no name
//but second para name is completion value = nil
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)saveDeviceType:(id)sender {
NSManagedObjectContext * context=[self managedObjectContext];
if (self.devicetypes)
{
[self.devicetypes setValue:self.deviceName.text forKey:@"Name"];
}
else
{
NSManagedObject * newDeviceType = [NSEntityDescription

insertNewObjectForEntityForName:@"DeviceType" inManagedObjectContext:context];

[newDeviceType setValue:self.deviceName.text forKey:@"name"];
}
NSError *error=nil;
if(![context save:&error])
{
NSLog(@"Cannot save! %@ %@",error,[error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
@end

-----------------------------------result---------------------------------

Screen Shot 2556-07-09 at 3.40.15 PM