secret

Navigation View Controller question

  • Download Gist
ContactsTableViewController.h
Objective-C
1 2 3 4 5 6 7 8 9 10
#import <UIKit/UIKit.h>
#import "CoreDataTableViewController.h"
 
@interface ContactsTableViewController : UITableViewController<UINavigationControllerDelegate> {
UINavigationController *navController;
}
 
@property (nonatomic, strong) NSArray *contacts;
 
@end
ContactsTableViewController.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.