Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DetailViewController.h
- #import <UIKit/UIKit.h>
- #import <CoreData/CoreData.h>
- @class RootViewController;
- @interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate>
- @property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
- @property (nonatomic, retain) NSManagedObject *detailItem;
- @property (nonatomic, retain) IBOutlet UILabel *detailDescriptionLabel;
- @property (nonatomic, assign) IBOutlet RootViewController *rootViewController;
- - (IBAction)insertNewObject:(id)sender;
- @end
- // DetailViewController.m
- #import "DetailViewController.h"
- #import "RootViewController.h"
- @interface DetailViewController ()
- @property (nonatomic, retain) UIPopoverController *popoverController;
- - (void)configureView;
- @end
- @implementation DetailViewController
- @synthesize toolbar = _toolbar;
- @synthesize detailItem = _detailItem;
- @synthesize detailDescriptionLabel = _detailDescriptionLabel;
- @synthesize popoverController = _myPopoverController;
- @synthesize rootViewController = _rootViewController;
- #pragma mark - Managing the detail item
- /*
- When setting the detail item, update the view and dismiss the popover controller if it's showing.
- */
- - (void)setDetailItem:(NSManagedObject *)managedObject
- {
- if (_detailItem != managedObject) {
- [_detailItem release];
- _detailItem = [managedObject retain];
- // Update the view.
- [self configureView];
- }
- if (self.popoverController != nil) {
- [self.popoverController dismissPopoverAnimated:YES];
- }
- }
- - (void)configureView
- {
- // Update the user interface for the detail item.
- NSLog(@"9999");
- // Normally should use accessor method, but using KVC here avoids adding a custom class to the template.
- self.detailDescriptionLabel.text = [[self.detailItem valueForKey:@"timeStamp"] description];
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- }
- - (void)viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
- }
- - (void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- }
- - (void)viewDidDisappear:(BOOL)animated
- {
- [super viewDidDisappear:animated];
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- return YES;
- }
- #pragma mark - Split view support
- - (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController: (UIPopoverController *)pc
- {
- barButtonItem.title = @"Events";
- NSMutableArray *items = [[self.toolbar items] mutableCopy];
- [items insertObject:barButtonItem atIndex:0];
- [self.toolbar setItems:items animated:YES];
- [items release];
- self.popoverController = pc;
- }
- // Called when the view is shown again in the split view, invalidating the button and popover controller.
- - (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
- {
- NSMutableArray *items = [[self.toolbar items] mutableCopy];
- [items removeObjectAtIndex:0];
- [self.toolbar setItems:items animated:YES];
- [items release];
- self.popoverController = nil;
- }
- /*
- // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- }
- */
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- self.popoverController = nil;
- }
- #pragma mark - Memory management
- - (void)didReceiveMemoryWarning
- {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- - (void)dealloc
- {
- [_myPopoverController release];
- [_toolbar release];
- [_detailItem release];
- [_detailDescriptionLabel release];
- [super dealloc];
- }
- #pragma mark - Object insertion
- - (IBAction)insertNewObject:(id)sender
- {
- [self.rootViewController insertNewObject:sender];
- }
- @end
- // RootViewController.h
- #import <UIKit/UIKit.h>
- #import "CoreDataHelper.h"
- #import "File.h"
- @class DetailViewController;
- @interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate>{
- NSManagedObjectContext *managedObjectContext;
- NSMutableArray *entityArray;
- NSString *entityName;
- NSPredicate *entitySearchPredicate;
- }
- -(void) loadDocuments;
- @property (nonatomic, retain) NSPredicate *entitySearchPredicate;
- @property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;
- @property (nonatomic, retain) NSString *entityName;
- @property (nonatomic, retain) NSMutableArray *entityArray;
- @property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
- @property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
- @end
- // RootViewController.m
- #import "RootViewController.h"
- #import "DetailViewController.h"
- @implementation RootViewController
- @synthesize detailViewController;
- @synthesize fetchedResultsController;
- @synthesize managedObjectContext;
- @synthesize entityName;
- @synthesize entitySearchPredicate;
- @synthesize entityArray;
- - (void)viewDidLoad
- {
- NSLog(@"Loading Docs");
- self.title = self.entityName;
- self.clearsSelectionOnViewWillAppear = NO;
- self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
- if(self.entityArray == nil){
- [self loadDocuments];
- }
- [super viewDidLoad];
- }
- -(void) loadDocuments {
- if(entitySearchPredicate == nil){
- NSMutableArray* mutableFetchResults = [CoreDataHelper getObjectsFromContext:self.entityName :@"name" :YES :managedObjectContext];
- self.entityArray = mutableFetchResults;
- }
- else
- {
- NSMutableArray* mutableFetchResults = [CoreDataHelper searchObjectsInContext:self.entityName :entitySearchPredicate :@"name" :YES :managedObjectContext];
- self.entityArray = mutableFetchResults;
- }
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- }
- - (void)viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
- }
- - (void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- }
- - (void)viewDidDisappear:(BOOL)animated
- {
- [super viewDidDisappear:animated];
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- return YES;
- }
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- - (void)viewDidUnload {
- // Release anything that can be recreated in viewDidLoad or on demand.
- // e.g. self.myOutlet = nil;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return [entityArray count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSManagedObject *object = (NSManagedObject *)[entityArray objectAtIndex:indexPath.row];
- NSString *CellIdentifier = @"Cell";
- int indicator = UITableViewCellAccessoryNone;
- if(self.entityName == @"Category"){
- if([[object valueForKey:@"SubCategory"] count] > 0){
- indicator = UITableViewCellAccessoryDisclosureIndicator;
- CellIdentifier = @"CellWithDisclosure";
- }
- } else if(self.entityName == @"SubCategory") {
- if([[object valueForKey:@"File"] count] > 0){
- indicator = UITableViewCellAccessoryDisclosureIndicator;
- CellIdentifier = @"CellWithDisclosure";
- }
- }
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
- }
- cell.accessoryType = indicator;
- cell.textLabel.text = [object valueForKey:@"name"];
- return cell;
- }
- - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
- {
- // The table view should not be re-orderable.
- return NO;
- }
- - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- // Set the detail item in the detail view controller.
- if(self.entityName == @"Category" || self.entityName == @"SubCategory")
- {
- // Get the object the user selected from the array
- NSManagedObject *selectedObject = [entityArray objectAtIndex:indexPath.row];
- // Create a new table view of this very same class.
- RootViewController *rootViewController = [[RootViewController alloc]
- initWithStyle:UITableViewStylePlain];
- // Pass the managed object context
- rootViewController.managedObjectContext = self.managedObjectContext;
- NSPredicate *predicate = nil;
- if(self.entityName == @"Category")
- {
- rootViewController.entityName = @"SubCategory";
- // Create a query predicate to find all child objects of the selected object.
- predicate = [NSPredicate predicateWithFormat:@"(Category == %@)", selectedObject];
- }
- else if(self.entityName == @"SubCategory")
- {
- rootViewController.entityName = @"File";
- // Create a query predicate to find all child objects of the selected object.
- predicate = [NSPredicate predicateWithFormat:@"(SubCategory == %@)", selectedObject];
- }
- [rootViewController setEntitySearchPredicate:predicate];
- //Push the new table view on the stack
- [self.navigationController pushViewController:rootViewController animated:YES];
- [rootViewController release];
- }
- else if(self.entityName == @"File")
- {
- // Get the object the user selected from the array
- File *selectedFile = [entityArray objectAtIndex:indexPath.row];
- //detailViewController.source = selectedFile.source;
- detailViewController.detailItem = selectedFile;
- NSLog(@"CLICKY");
- }
- }
- - (void)didReceiveMemoryWarning
- {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Relinquish ownership any cached data, images, etc that aren't in use.
- }
- - (void)dealloc
- {
- if(entitySearchPredicate != nil)
- {
- [entitySearchPredicate release];
- }
- [detailViewController release];
- [fetchedResultsController release];
- [managedObjectContext release];
- [entityArray release];
- [entityName release];
- [super dealloc];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement