PDA

View Full Version : UIActionSheet causes App to Crash




sheepopo39
Aug 19, 2010, 01:17 PM
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

terminate called after throwing an instance of 'NSException'

Here is the code for UsingViewController.m:

//
// 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

#import <UIKit/UIKit.h>

@interface UsingViewsViewController : UIViewController
<UIActionSheetDelegate> {

}

@end

I am absolutely stumped as to why this is causing the app to crash.



robbieduncan
Aug 19, 2010, 01:33 PM
The run-console should have more detail on the NSException...

sheepopo39
Aug 19, 2010, 01:40 PM
Oh, sorry, hope this helps.

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'

robbieduncan
Aug 19, 2010, 01:44 PM
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.

sheepopo39
Aug 19, 2010, 01:47 PM
Well there you go: the view is nill. The issue is the line:


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.

What should I do to fix it?

robbieduncan
Aug 19, 2010, 02:06 PM
What should I do to fix it?

Supply a non-nil view. Without knowing your entire app design (don't tell me: I'm not going to read it and think about it) I have no idea what view.

sherry2009
Aug 19, 2010, 09:37 PM
//
// 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]);
[actionSheet release];
}

// 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];
}
@end

dejo
Aug 19, 2010, 09:47 PM
sherry2009, that still doesn't address the nil-view issue.

sherry2009
Aug 20, 2010, 12:10 AM
where are you allocate the UsingViewsViewController like this:
UsingViewsViewController* viewcontro=[[UsingViewsViewController alloc]init];