-
-
Save paxswill/d83a3eb6fdfa0d285955 to your computer and use it in GitHub Desktop.
Navigation View Controller question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
#import "CoreDataTableViewController.h" | |
@interface ContactsTableViewController : UITableViewController<UINavigationControllerDelegate> { | |
UINavigationController *navController; | |
} | |
@property (nonatomic, strong) NSArray *contacts; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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