Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.