Skip to content

Instantly share code, notes, and snippets.

@paxswill
Created January 18, 2012 20:01
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 paxswill/d83a3eb6fdfa0d285955 to your computer and use it in GitHub Desktop.
Save paxswill/d83a3eb6fdfa0d285955 to your computer and use it in GitHub Desktop.
Navigation View Controller question
#import <UIKit/UIKit.h>
#import "CoreDataTableViewController.h"
@interface ContactsTableViewController : UITableViewController<UINavigationControllerDelegate> {
UINavigationController *navController;
}
@property (nonatomic, strong) NSArray *contacts;
@end
#import "ContactsTableViewController.h"
#import "AddressBookAppDelegate.h"
#import "ContactDetailViewController.h"
#import "Contact.h"
@implementation ContactsTableViewController
@synthesize contacts = _contacts;
- (void)fetchData
{
AddressBookAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact"
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = entity;
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSError *error = nil;
self.contacts = [context executeFetchRequest:request error:&error];
if(error == nil){
[self.tableView reloadData];
}
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
self.navigationController.delegate = self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Show Contact"]) {
Contact *contact = [self.contacts objectAtIndex:[self.tableView.indexPathForSelectedRow row]];
[segue.destinationViewController setContact:contact];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.contacts count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Contact Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
Contact *contact = [self.contacts objectAtIndex:[indexPath row]];
cell.textLabel.text = contact.name;
return cell;
}
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
[self fetchData];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment