Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sheepopo39

macrumors 6502
Original poster
Sep 18, 2008
251
0
Hi, I'm working through the book "Beginning iPad Development" and it tells me to create an action sheet, and have a couple buttons displayed on it. I get NO errors or warning but when I click build and run, the app automatically crashes and I get this in the debugger

Code:
terminate called after throwing an instance of 'NSException'

Here is the code for UsingViewController.m:

Code:
//
//  UsingViewsViewController.m
//  UsingViews
//
//  Created by John on 10-08-19.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "UsingViewsViewController.h"

@implementation UsingViewsViewController

- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex
{
	NSLog(@"%@", [NSString stringWithFormat:@"%d", buttonIndex]);
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
	
	UIActionSheet *action = [[UIActionSheet alloc]
							 initWithTitle:@"Title of Action Sheet" 
							 delegate:self 
							 cancelButtonTitle:@"OK" 
							 destructiveButtonTitle:@"Delete Message" 
							 otherButtonTitles:@"Option 1", @"Option 2", nil];
	
	[action showInView:self.view];
	[action release];
	
    [super viewDidLoad];
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

And for UsingViewController.h

Code:
#import <UIKit/UIKit.h>

@interface UsingViewsViewController : UIViewController 
<UIActionSheetDelegate> {

}

@end

I am absolutely stumped as to why this is causing the app to crash.
 
Oh, sorry, hope this helps.

Code:
2010-08-19 14:37:53.368 UsingViews[2030:207] *** Assertion failure in -[UIActionSheet showInView:], /SourceCache/UIKit_Sim/UIKit-1145.66/UIAlert.m:7073
2010-08-19 14:37:53.372 UsingViews[2030:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: view != nil'
2010-08-19 14:37:53.374 UsingViews[2030:207] Stack: (
    42174544,
    43332396,
    41912075,
    713492,
    3697969,
    3764256,
    9443,
    3518341,
    8677,
    2902340,
    2906447,
    2932030,
    2913527,
    2945496,
    51167612,
    41453724,
    41449640,
    2904609,
    2937714,
    8568,
    8422
)
terminate called after throwing an instance of 'NSException'
 
Well there you go: the view is nill. The issue is the line:
[action showInView:self.view];

self.view is nil

In all seriousness you should be able to work that out from the Exception. It says exactly what the problem is.
 
Code:
//
//  UsingViewsViewController.m
//  UsingViews
//
//  Created by John on 10-08-19.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "UsingViewsViewController.h"

@implementation UsingViewsViewController

- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex
{
	NSLog(@"%@", [NSString stringWithFormat:@"%d", buttonIndex]);
        [COLOR="Red"][actionSheet release];[/COLOR]
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
	
	UIActionSheet *action = [[UIActionSheet alloc]
							 initWithTitle:@"Title of Action Sheet" 
							 delegate:self 
							 cancelButtonTitle:@"OK" 
							 destructiveButtonTitle:@"Delete Message" 
							 otherButtonTitles:@"Option 1", @"Option 2", nil];
	
	[action showInView:self.view];
	[COLOR="red"]//[action release];[/COLOR]
	
    [super viewDidLoad];
}
@end
 
where are you allocate the UsingViewsViewController like this:
Code:
UsingViewsViewController* viewcontro=[[UsingViewsViewController alloc]init];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.