Skip to content

Instantly share code, notes, and snippets.

@naoyamakino
Created October 25, 2012 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naoyamakino/3955875 to your computer and use it in GitHub Desktop.
Save naoyamakino/3955875 to your computer and use it in GitHub Desktop.
ViewController.m from CoreData tutorial http://www.youtube.com/watch?v=mEAav7dM4hk
//
// ViewController.m
// coreDataTutorial
//
// Created by Naoya Makino on 12-10-25.
// Copyright (c) 2012 Naoya Makino. All rights reserved.
//
#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()
{
NSManagedObjectContext *context;
}
@end
@implementation ViewController
@synthesize firstnameTextField;
@synthesize lastnameTextField;
@synthesize displayLabel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[self firstnameTextField]setDelegate:self];
[[self lastnameTextField]setDelegate:self];
}
- (void)viewDidUnload
{
[self setFirstnameTextField:nil];
[self setLastnameTextField:nil];
AppDelegate *appdelegate = [[UIApplication sharedApplication]delegate];
context = [appdelegate managedObjectContext];
[self setDisplayLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)addPersonButton:(id)sender {
NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
NSManagedObject *newPerson = [[NSManagedObject alloc]initWithEntity:entitydesc insertIntoManagedObjectContext:context];
[newPerson setValue:self.firstnameTextField.text forKey:@"firstname"];
[newPerson setValue:self.lastnameTextField.text forKey:@"lastname"];
NSError *error;
[context save:&error];
self.displayLabel.text = @"Person added.";
}
- (IBAction)searchPersonButton:(id)sender {
NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entitydesc];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstname Like %@ and lastname like %@", self.firstnameTextField.text, self.lastnameTextField.text];
[request setPredicate:predicate];
NSError *error;
NSArray *matchingData = [context executeFetchRequest:request error:&error];
if (matchingData.count <= 0)
{
self.displayLabel.text = @"No person found.";
}
else {
NSString *firstName;
NSString *lastName;
for (NSManagedObject *obj in matchingData) {
firstName = [obj valueForKey:@"firstname"];
lastName = [obj valueForKey:@"lastname"];
}
self.displayLabel.text = [NSString stringWithFormat:@"Firstname: %@, Lastname: %@", firstName, lastName];
}
}
- (IBAction)deletePersonButton:(id)sender {
NSEntityDescription *entityDec = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entityDec];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstname Like %@", self.firstnameTextField.text];
[request setPredicate:predicate];
NSError *error;
NSArray *matchingData = [context executeFetchRequest:request error:&error];
if(matchingData.count <= 0)
{
self.displayLabel.text = @"no person deleted.";
}
else
{
int count = 0;
for (NSManagedObject *obj in matchingData) {
[context deleteObject:obj];
++count;
}
[context save:&error];
self.displayLabel.text = [NSString stringWithFormat:@"%d persons deleted", count];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment