Advertisement
Guest User

Untitled

a guest
Aug 30th, 2011
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //  DetailViewController.h
  2. #import <UIKit/UIKit.h>
  3.  
  4. #import <CoreData/CoreData.h>
  5.  
  6. @class RootViewController;
  7.  
  8. @interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate>
  9.  
  10. @property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
  11.  
  12. @property (nonatomic, retain) NSManagedObject *detailItem;
  13.  
  14. @property (nonatomic, retain) IBOutlet UILabel *detailDescriptionLabel;
  15.  
  16. @property (nonatomic, assign) IBOutlet RootViewController *rootViewController;
  17.  
  18. - (IBAction)insertNewObject:(id)sender;
  19.  
  20. @end
  21.  
  22. //  DetailViewController.m
  23.  
  24.  
  25. #import "DetailViewController.h"
  26.  
  27. #import "RootViewController.h"
  28.  
  29. @interface DetailViewController ()
  30. @property (nonatomic, retain) UIPopoverController *popoverController;
  31. - (void)configureView;
  32. @end
  33.  
  34. @implementation DetailViewController
  35.  
  36. @synthesize toolbar = _toolbar;
  37. @synthesize detailItem = _detailItem;
  38. @synthesize detailDescriptionLabel = _detailDescriptionLabel;
  39. @synthesize popoverController = _myPopoverController;
  40. @synthesize rootViewController = _rootViewController;
  41.  
  42. #pragma mark - Managing the detail item
  43.  
  44. /*
  45.  When setting the detail item, update the view and dismiss the popover controller if it's showing.
  46.  */
  47. - (void)setDetailItem:(NSManagedObject *)managedObject
  48. {
  49.     if (_detailItem != managedObject) {
  50.         [_detailItem release];
  51.         _detailItem = [managedObject retain];
  52.        
  53.         // Update the view.
  54.         [self configureView];
  55.     }
  56.    
  57.     if (self.popoverController != nil) {
  58.         [self.popoverController dismissPopoverAnimated:YES];
  59.     }      
  60. }
  61.  
  62. - (void)configureView
  63. {
  64.     // Update the user interface for the detail item.
  65.     NSLog(@"9999");
  66.     // Normally should use accessor method, but using KVC here avoids adding a custom class to the template.
  67.     self.detailDescriptionLabel.text = [[self.detailItem valueForKey:@"timeStamp"] description];
  68. }
  69.  
  70. - (void)viewWillAppear:(BOOL)animated
  71. {
  72.     [super viewWillAppear:animated];
  73. }
  74.  
  75. - (void)viewDidAppear:(BOOL)animated
  76. {
  77.     [super viewDidAppear:animated];
  78. }
  79.  
  80. - (void)viewWillDisappear:(BOOL)animated
  81. {
  82.     [super viewWillDisappear:animated];
  83. }
  84.  
  85. - (void)viewDidDisappear:(BOOL)animated
  86. {
  87.     [super viewDidDisappear:animated];
  88. }
  89.  
  90. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  91.     return YES;
  92. }
  93.  
  94. #pragma mark - Split view support
  95.  
  96. - (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController: (UIPopoverController *)pc
  97. {
  98.     barButtonItem.title = @"Events";
  99.     NSMutableArray *items = [[self.toolbar items] mutableCopy];
  100.     [items insertObject:barButtonItem atIndex:0];
  101.     [self.toolbar setItems:items animated:YES];
  102.     [items release];
  103.     self.popoverController = pc;
  104. }
  105.  
  106. // Called when the view is shown again in the split view, invalidating the button and popover controller.
  107. - (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
  108. {
  109.     NSMutableArray *items = [[self.toolbar items] mutableCopy];
  110.     [items removeObjectAtIndex:0];
  111.     [self.toolbar setItems:items animated:YES];
  112.     [items release];
  113.     self.popoverController = nil;
  114. }
  115.  
  116. /*
  117.  // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  118. - (void)viewDidLoad
  119. {
  120.     [super viewDidLoad];
  121. }
  122.  */
  123.  
  124. - (void)viewDidUnload
  125. {
  126.     [super viewDidUnload];
  127.  
  128.     // Release any retained subviews of the main view.
  129.     // e.g. self.myOutlet = nil;
  130.     self.popoverController = nil;
  131. }
  132.  
  133. #pragma mark - Memory management
  134.  
  135. - (void)didReceiveMemoryWarning
  136. {
  137.     // Releases the view if it doesn't have a superview.
  138.     [super didReceiveMemoryWarning];
  139.    
  140.     // Release any cached data, images, etc that aren't in use.
  141. }
  142.  
  143. - (void)dealloc
  144. {
  145.     [_myPopoverController release];
  146.     [_toolbar release];
  147.     [_detailItem release];
  148.     [_detailDescriptionLabel release];
  149.     [super dealloc];
  150. }
  151.  
  152. #pragma mark - Object insertion
  153.  
  154. - (IBAction)insertNewObject:(id)sender
  155. {
  156.     [self.rootViewController insertNewObject:sender];  
  157. }
  158.  
  159. @end
  160.  
  161. //  RootViewController.h
  162. #import <UIKit/UIKit.h>
  163. #import "CoreDataHelper.h"
  164. #import "File.h"
  165.  
  166. @class DetailViewController;
  167.  
  168. @interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate>{
  169.     NSManagedObjectContext *managedObjectContext;
  170.     NSMutableArray *entityArray;
  171.     NSString *entityName;
  172.     NSPredicate *entitySearchPredicate;
  173. }
  174.  
  175. -(void) loadDocuments;
  176.  
  177. @property (nonatomic, retain) NSPredicate *entitySearchPredicate;
  178. @property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;
  179. @property (nonatomic, retain) NSString *entityName;
  180. @property (nonatomic, retain) NSMutableArray *entityArray;
  181. @property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
  182. @property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
  183.  
  184. @end
  185.  
  186. //  RootViewController.m
  187.  
  188.  
  189. #import "RootViewController.h"
  190.  
  191. #import "DetailViewController.h"
  192.  
  193. @implementation RootViewController
  194.        
  195. @synthesize detailViewController;
  196. @synthesize fetchedResultsController;
  197. @synthesize managedObjectContext;
  198. @synthesize entityName;
  199. @synthesize entitySearchPredicate;
  200. @synthesize entityArray;
  201.  
  202. - (void)viewDidLoad
  203. {
  204.     NSLog(@"Loading Docs");
  205.     self.title = self.entityName;
  206.     self.clearsSelectionOnViewWillAppear = NO;
  207.     self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
  208.     if(self.entityArray == nil){
  209.         [self loadDocuments];
  210.     }
  211.     [super viewDidLoad];
  212. }
  213.  
  214. -(void) loadDocuments {
  215.     if(entitySearchPredicate == nil){
  216.         NSMutableArray* mutableFetchResults = [CoreDataHelper getObjectsFromContext:self.entityName :@"name" :YES :managedObjectContext];
  217.         self.entityArray = mutableFetchResults;
  218.     }
  219.     else
  220.     {
  221.         NSMutableArray* mutableFetchResults = [CoreDataHelper searchObjectsInContext:self.entityName :entitySearchPredicate :@"name" :YES :managedObjectContext];
  222.         self.entityArray = mutableFetchResults;
  223.     }
  224.    
  225. }
  226. - (void)viewWillAppear:(BOOL)animated
  227. {
  228.     [super viewWillAppear:animated];
  229. }
  230.  
  231. - (void)viewDidAppear:(BOOL)animated
  232. {
  233.     [super viewDidAppear:animated];
  234. }
  235.  
  236. - (void)viewWillDisappear:(BOOL)animated
  237. {
  238.     [super viewWillDisappear:animated];
  239. }
  240.  
  241. - (void)viewDidDisappear:(BOOL)animated
  242. {
  243.     [super viewDidDisappear:animated];
  244. }
  245.  
  246. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  247.     return YES;
  248. }
  249.  
  250. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  251. {
  252.     return 1;
  253. }
  254.  
  255. - (void)viewDidUnload {
  256.     // Release anything that can be recreated in viewDidLoad or on demand.
  257.     // e.g. self.myOutlet = nil;
  258. }
  259.  
  260. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  261. {
  262.    return [entityArray count];
  263. }
  264.  
  265.        
  266. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  267. {
  268.    
  269.     NSManagedObject *object = (NSManagedObject *)[entityArray objectAtIndex:indexPath.row];
  270.    
  271.     NSString *CellIdentifier = @"Cell";
  272.     int indicator = UITableViewCellAccessoryNone;
  273.     if(self.entityName == @"Category"){
  274.         if([[object valueForKey:@"SubCategory"] count] > 0){
  275.             indicator = UITableViewCellAccessoryDisclosureIndicator;
  276.             CellIdentifier = @"CellWithDisclosure";
  277.         }
  278.     } else if(self.entityName == @"SubCategory") {
  279.         if([[object valueForKey:@"File"] count] > 0){
  280.             indicator = UITableViewCellAccessoryDisclosureIndicator;
  281.             CellIdentifier = @"CellWithDisclosure";
  282.         }
  283.     }
  284.    
  285.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  286.     if (cell == nil) {
  287.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  288.     }
  289.    
  290.     cell.accessoryType = indicator;
  291.     cell.textLabel.text = [object valueForKey:@"name"];
  292.     return cell;
  293. }
  294.  
  295.  
  296. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  297. {
  298.     // The table view should not be re-orderable.
  299.     return NO;
  300. }
  301.  
  302. - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  303. {
  304.     // Set the detail item in the detail view controller.
  305.     if(self.entityName == @"Category" || self.entityName == @"SubCategory")
  306.     {
  307.         // Get the object the user selected from the array
  308.         NSManagedObject *selectedObject = [entityArray objectAtIndex:indexPath.row];
  309.        
  310.         // Create a new table view of this very same class.
  311.         RootViewController *rootViewController = [[RootViewController alloc]
  312.                                                   initWithStyle:UITableViewStylePlain];
  313.        
  314.         // Pass the managed object context
  315.         rootViewController.managedObjectContext = self.managedObjectContext;
  316.         NSPredicate *predicate = nil;
  317.        
  318.        
  319.         if(self.entityName == @"Category")
  320.         {
  321.             rootViewController.entityName = @"SubCategory";
  322.            
  323.             // Create a query predicate to find all child objects of the selected object.
  324.             predicate = [NSPredicate predicateWithFormat:@"(Category == %@)", selectedObject];
  325.            
  326.         }
  327.         else if(self.entityName == @"SubCategory")
  328.         {
  329.             rootViewController.entityName = @"File";
  330.            
  331.             // Create a query predicate to find all child objects of the selected object.
  332.             predicate = [NSPredicate predicateWithFormat:@"(SubCategory == %@)", selectedObject];
  333.         }
  334.         [rootViewController setEntitySearchPredicate:predicate];
  335.        
  336.         //Push the new table view on the stack
  337.         [self.navigationController pushViewController:rootViewController animated:YES];
  338.         [rootViewController release];
  339.     }
  340.     else if(self.entityName == @"File")
  341.     {
  342.         // Get the object the user selected from the array
  343.         File *selectedFile = [entityArray objectAtIndex:indexPath.row];
  344.         //detailViewController.source = selectedFile.source;
  345.         detailViewController.detailItem = selectedFile;
  346.         NSLog(@"CLICKY");
  347.     }
  348.    
  349. }
  350.  
  351. - (void)didReceiveMemoryWarning
  352. {
  353.     // Releases the view if it doesn't have a superview.
  354.     [super didReceiveMemoryWarning];
  355.  
  356.     // Relinquish ownership any cached data, images, etc that aren't in use.
  357. }
  358.  
  359. - (void)dealloc
  360. {
  361.     if(entitySearchPredicate != nil)
  362.     {
  363.         [entitySearchPredicate release];
  364.     }
  365.     [detailViewController release];
  366.     [fetchedResultsController release];
  367.     [managedObjectContext release];
  368.     [entityArray release];
  369.     [entityName release];
  370.     [super dealloc];
  371. }
  372.  
  373. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement