Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-12-2010, 10:46 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 24
snorkelt is on a distinguished road
Default crashing...viewController problem?

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
snorkelt is offline   Reply With Quote
Old 09-12-2010, 10:52 PM   #2 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 24
snorkelt is on a distinguished road
Default

I see that there is a typo in my code above (a result of my trying to simplify it for posting):

table_To_Enter_First_TriView = [[Table_To_Enter_First_TriViewController alloc] initWithParentViewController:self];

should say:

table_To_Enter_First_TriView = [[TriViewController alloc] initWithParentViewController:self];

It is correct in the code that I am trying to run (which is crashing). Sorry about that.
snorkelt is offline   Reply With Quote
Old 09-13-2010, 03:11 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Enschede, Netherlands
Posts: 198
rickrets is on a distinguished road
Default

Try commenting this line out (inside - (id)init):
Code:
[calculateButtonItem release];
I wouldn't call myself experienced, but I had some weird errors like you are having, and most of the time it had something to do with releasing an object..
rickrets is offline   Reply With Quote
Old 09-13-2010, 04:08 AM   #4 (permalink)
Registered Member
 
CambridgeCoders's Avatar
 
Join Date: Jul 2010
Location: Cambridge, UK
Posts: 73
CambridgeCoders is on a distinguished road
Default

Hi,

In the viewDidLoad method:

Code:
- (void)viewDidLoad {
	[self loadTable_to_enter_first_tri];
        [self loadNav];
    self.navigationController.navigationBarHidden=NO;
	[super viewDidLoad];	
}
I would suggest calling [super viewDidLoad] first, rather than last. This gives the opportunity for the superview to set itself up before you start adding your elements to your own view. I wonder if you have a problem essentially using the view before it's been set up correctly.

Let us know how you get on.

Best regards,
__________________
Andy, Cambridge Coders Limited, www.cambridgecoders.com
Calcuccino - scientific calculator for the iPhone
CambridgeCoders is offline   Reply With Quote
Old 09-13-2010, 09:06 AM   #5 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 24
snorkelt is on a distinguished road
Default

Thanks! I had tried both of these before and just tried again but no change in the crash...Is it something about calling -initWithParentViewController from -init? I tried to move it all into init but it still crashed, although perhaps in a slightly different place. Is the fact that I end up instantiating the view twice part of the problem?
snorkelt is offline   Reply With Quote
Old 09-13-2010, 11:49 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 24
snorkelt is on a distinguished road
Default

OK, I got it to run by making the following change, BUT...
Code:
[self.view addSubview:[table_To_Enter_First_TriView view]];
instead of
Code:
[self.view addSubview:table_To_Enter_First_TriView];
but I get the warning 'UIView' may not respond to '-view'.
What do I do to fix the warning but still get this to stop crashing?
snorkelt is offline   Reply With Quote
Reply

Bookmarks

Tags
tableview, viewcontroller

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 342
12 members and 330 guests
Absentia, cgokey, fiftysixty, givensur, iGamesDev, jenniead38, linkmx, mraalex, PixelInteractive, raihan.zbr, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:02 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0