Skip to content

Instantly share code, notes, and snippets.

@jamespember
Last active August 29, 2015 14:18
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 jamespember/4aee343d73ebcd7f7b3f to your computer and use it in GitHub Desktop.
Save jamespember/4aee343d73ebcd7f7b3f to your computer and use it in GitHub Desktop.
**JPAppDelegate.m**
@implementation JPAppDelegate
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
**In JPAppDelegate.h**
#import <UIKit/UIKit.h>
@interface JPAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@end
**In LeaderboardViewController.h**
#import <UIKit/UIKit.h>
#import "JPAppDelegate.h"
#import <CoreData/CoreData.h>
@interface LeaderboardViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, assign) int scoreToBeAdded;
@end
**In LeaderboardViewController.m**
#import "LeaderboardViewController.h"
#import "JPAppDelegate.h"
@interface LeaderboardViewController ()
@end
@implementation LeaderboardViewController
{
NSMutableArray *scores;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Initialize table data
scores = [NSMutableArray arrayWithObjects:@"TODO: Enter real data here", @"TODO: Enter real data here", nil];
NSNumber *anumber = [NSNumber numberWithInteger:_scoreToBeAdded];
NSString *scoreToAddAsString = [anumber stringValue];
JPAppDelegate *JPAppDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =[JPAppDelegate managedObjectContext];
NSLog(context);
NSManagedObject *newScore;
newScore = [NSEntityDescription
insertNewObjectForEntityForName:@"Scores"
inManagedObjectContext:context];
[newScore setValue: scoreToAddAsString forKey:@"score"];
NSError *error;
[context save:&error];
NSLog(@"Leaderboard loaded w new score to add %i", _scoreToBeAdded);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [scores count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [scores objectAtIndex:indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment