I have been banging my head and moving things around on this for a while. Maybe a (much more) experienced developer will immediately see the error of my ways?
From logs I can see that:
I enter the init method, which leads into the initWithParentViewController method. Then I go into viewDidLoad. Then, in initWithParentViewController, I proceed to:
[self.view addSubview:table_To_Enter_First_TriView];
and then back into viewDidLoad (for the second time).
After leaving viewDidLoad I immediately crash with:
....TriViewController superview]: unrecognized selector sent to instance...
I am providing most of the .m file here although I believe the problem to be in the init or initWithParentViewController methods.
I really appreciate your help!
Code:
// Header File
#import "TriViewController.h"
// View Controllers
#import "MainAppDelegate.h"
#import "PregnanciesViewController.h"
#import "MultipleViewController.h"
#import "SurgeryViewController.h"
#import "ComplicationsViewController.h"
#import "PredictionsViewController.h"
// Private Methods
@interface TriViewController()
- (void)loadTable_to_enter_first_tri;
- (void)loadNav;
@end
@implementation TriViewController
@synthesize table_To_Enter_First_TriView, table_to_enter_first_tri, table_to_enter_first_triFooterText, table_to_enter_first_triCellTextValues, table_to_enter_first_triCellDetailTextValues, table_to_enter_first_triSectionTextValues;
int table_to_enter_first_triSelectedRow;
BOOL table_to_enter_first_triShowHeader;
#pragma mark -
#pragma mark Memory Management
- (void)dealloc {
[table_to_enter_first_tri release];
[table_to_enter_first_triCellTextValues release];
[table_to_enter_first_triCellDetailTextValues release];
[table_to_enter_first_triSectionTextValues release];
[table_to_enter_first_triFooterText release];
[super dealloc];
}
#pragma mark -
#pragma mark Initialisation
- (id)init {
if (self = [super init]) {
self.title = @"History";
UIBarButtonItem *calculateButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Calculate"
style:UIBarButtonItemStyleBordered target:self action:@selector(calculateButtonAction)];
[self.navigationItem setRightBarButtonItem:calculateButtonItem];
table_To_Enter_First_TriView = [[Table_To_Enter_First_TriViewController alloc] initWithParentViewController:self];
//crashes here in this next line! it goes in and out of viewDidLoad method and then crashes at:
[self.view addSubview:table_To_Enter_First_TriView];
[calculateButtonItem release];
}
return self;
}
- (id)initWithParentViewController:(Table_To_Enter_First_TriViewController *)parent {
if (self = [super init]) {
[self.view setFrame:CGRectMake(0, 0, 320, 460)];
refParentViewController = parent;
[self.view setBackgroundColor:[UIColor lightGrayColor]];
return self;
}
#pragma mark -
#pragma mark Load Subview Methods
- (void)loadTable_to_enter_first_tri {
table_to_enter_first_tri = [[UITableView alloc] initWithFrame:CGRectMake(0, 81, 320, 236) style:UITableViewStyleGrouped];
[table_to_enter_first_tri setDataSource:self];
[table_to_enter_first_tri setDelegate:self];
table_to_enter_first_triCellTextValues = [[NSArray alloc] initWithObjects:@"W", @"X", @"Y", @"Z", nil];
MainAppDelegate *appDelegate = (MainAppDelegate *)[[UIApplication sharedApplication] delegate];
table_to_enter_first_triCellDetailTextValues = [[NSArray alloc] initWithObjects:@"w", @"x", appDelegate.had_surgery_string, appDelegate.has_DMorHTN_string, nil];
table_to_enter_first_triSectionTextValues = [[NSArray alloc] initWithObjects:@"Sample Section", nil];
table_to_enter_first_triSelectedRow = 0;
table_to_enter_first_triShowHeader = NO;
NSDate *third_trimester_starts = [appDelegate.user_entered_edc addTimeInterval: -24 * 60 * 60 * 30 * 3];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterShortStyle;
NSString *third_trimester_starts_string = [df stringFromDate:third_trimester_starts];
NSString *what_footer_label_should_say = [NSString stringWithFormat:@"Come back after %@ for an update", third_trimester_starts_string];
table_to_enter_first_triFooterText = what_footer_label_should_say;
[table_to_enter_first_tri setEditing:NO];
[table_to_enter_first_tri setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:table_to_enter_first_tri];
[df release];
}
- (void)loadNav {
UINavigationBar *nav = [[UINavigationBar alloc] init];
[nav setBarStyle:UIBarStyleDefault];
[nav setTranslucent:NO];
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"History"];
[navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]];
[navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"Calculate" style:UIBarButtonItemStyleDone target:nil action:nil]];
[nav setItems:[NSArray arrayWithObject:navigationItem]];
[navigationItem release];
[nav setFrame:CGRectMake(0, -44, 320, 44)];
[self.view addSubview:nav];
}
#pragma mark -
#pragma mark Action Methods
- (void)table_to_enter_first_triActionForRow:(NSInteger)row {
if (row == 0) {
PregnanciesViewController *controller = [[[PregnanciesViewController alloc] init] autorelease];
[self.navigationController pushViewController:controller animated:YES];
} else if (row == 1) {
MultipleViewController *controller = [[[MultipleViewController alloc] init] autorelease];
[self.navigationController pushViewController:controller animated:YES];
} else if (row == 2) {
SurgeryViewController *controller = [[[SurgeryViewController alloc] init] autorelease];
[self.navigationController pushViewController:controller animated:YES];
} else if (row == 3) {
ComplicationsViewController *controller = [[[ComplicationsViewController alloc] init] autorelease];
[self.navigationController pushViewController:controller animated:YES];
}
}
-(void)calculateButtonAction{
PredictionsViewController *controller = [[[PredictionsViewController alloc] init] autorelease];
[self.navigationController pushViewController:controller animated:YES];
}
#pragma mark -
#pragma mark TableView Delegates
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
return [table_to_enter_first_triCellTextValues count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [table_to_enter_first_triSectionTextValues count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (table_to_enter_first_triShowHeader == TRUE) {
return [table_to_enter_first_triSectionTextValues objectAtIndex:section];
}
return nil;
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
if ([table_to_enter_first_triFooterText length] > 0) {
return table_to_enter_first_triFooterText;
}
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [table_to_enter_first_triCellTextValues objectAtIndex:indexPath.row];
if (indexPath.row < [table_to_enter_first_triCellDetailTextValues count]) {
cell.detailTextLabel.text = [table_to_enter_first_triCellDetailTextValues objectAtIndex:indexPath.row];
}
if (indexPath.row == table_to_enter_first_triSelectedRow - 1) {
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
// Handle user selecting row event
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 0:
[refParentViewController table_to_enter_first_triActionForRow:indexPath.row];
// Deselect the row that the user has tapped
[tableView deselectRowAtIndexPath:indexPath animated:YES];
break;
case 1:
[refParentViewController table_to_enter_first_triActionForRow:indexPath.row];
// Deselect the row that the user has tapped
[tableView deselectRowAtIndexPath:indexPath animated:YES];
break;
case 2:
[refParentViewController table_to_enter_first_triActionForRow:indexPath.row];
// Deselect the row that the user has tapped
[tableView deselectRowAtIndexPath:indexPath animated:YES];
break;
case 3:
[refParentViewController table_to_enter_first_triActionForRow:indexPath.row];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
break;
default:
break;
}
}
#pragma mark -
#pragma mark UIViewController Delegates
- (void)viewWillAppear:(BOOL)animated {
[self.table_to_enter_first_tri reloadData];
deselectRowAtIndexPath:self.table_to_enter_first_tri.indexPathForSelectedRow animated:NO];
}
- (void)viewDidLoad {
[self loadTable_to_enter_first_tri];
[self loadNav];
self.navigationController.navigationBarHidden=NO;
[super viewDidLoad];
}
@end