UIActionSheet causes App to Crash

Discussion in 'iPhone/iPad Programming' started by sheepopo39, Aug 19, 2010.

  1. macrumors 6502

    Joined:
    Sep 18, 2008
    #1
    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.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    The run-console should have more detail on the NSException...
     
  3. thread starter macrumors 6502

    Joined:
    Sep 18, 2008
    #3
    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'
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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.
     
  5. thread starter macrumors 6502

    Joined:
    Sep 18, 2008
    #5
    What should I do to fix it?
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    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.
     
  7. macrumors newbie

    sherry2009

    Joined:
    Dec 14, 2009
    #7
    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
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    sherry2009, that still doesn't address the nil-view issue.
     
  9. macrumors newbie

    sherry2009

    Joined:
    Dec 14, 2009
    #9
    where are you allocate the UsingViewsViewController like this:
    Code:
    UsingViewsViewController* viewcontro=[[UsingViewsViewController alloc]init];
     

Share This Page