Assertion failure in -[UIActionSheet showInView:]

2.7k views Asked by At

Anyone have any ideas what this means and how to sort it out?

* Assertion failure in -[UIActionSheet showInView:], /SourceCache/UIKit_Sim/UIKit-1912.3/UIActionSheet.m:4630 2012-03-02 17:12:34.643 Strategy & Risk[66854:15803] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: view != nil'

Update: It only happens sporadically:

Update 2: I don't have a tab bar or tool bar in my app

Update 3: I have changed the code to use showInView: and I get the exact same error message.

- (void)displayAddEntityActionSheet {

    //Convert the tap location to this view's coordinate systems
    CGRect buttonTappedRect = [self.currentNodeView convertRect:self.currentNodeView.frame toView:self.view];

    UIActionSheet *actionSheet;

    switch (self.currentNode.nodeType) {
        case NodeTypeEntity:
            actionSheet = [[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil 
                                        destructiveButtonTitle:nil otherButtonTitles:@"Entity", @"Objective", nil] autorelease];

            [actionSheet showFromRect:buttonTappedRect inView:self.view animated:YES];
            break;
        case NodeTypeObjective:
            actionSheet = [[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil 
                                        destructiveButtonTitle:nil otherButtonTitles:@"Risk", @"KPI", nil] autorelease];

            [actionSheet showFromRect:buttonTappedRect inView:self.view animated:YES];
            break;
        case NodeTypeRisk:
            actionSheet = [[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil 
                                        destructiveButtonTitle:nil otherButtonTitles:@"KRI", @"Control", nil] autorelease];

            [actionSheet showFromRect:buttonTappedRect inView:self.view animated:YES];
            break;
        case NodeTypeControl:
            actionSheet = [[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil 
                                        destructiveButtonTitle:nil otherButtonTitles:@"KCI", nil] autorelease];

            [actionSheet showFromRect:buttonTappedRect inView:self.view animated:YES];
            break;
        default:
            break;
    }
}
3

There are 3 answers

0
TheLearner On BEST ANSWER

Finally discovered what was causing the error, the code below was being called several times which in turn called the method in my original post and this view controller was never being deallocated but even if it was I was not removing it as an observer for these notifications:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addNewEntity:) name:@"addSubEntity" object:nil];
    }

    return self;
}

So I have fixed the bug that calls init repeatedly and for safely sake I also call removeObserver: just in case, although this view controller is re-used so it never gets called.

Moral of the story: removeObserver before deallocating!

- (void)viewDidUnload
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
9
Parth Bhatt On

You should use following methods for showing action sheets if you have a tab bar in your application.

[actionsheet showFromTabBar:]

or

[actionsheet showFromToolbar:]

This assertion failure you are facing is because you might be having an tab bar in your application and in iPhone you might be showing the action sheet using the method:

[actionsheet showInView:]

If that is the case then you should use:

[actionsheet showFromTabBar:]

Hope this helps you.

EDIT-2:

[actionsheet showFromBarButtonItem:self.navigationItem.rightBarButtonItem animated:YES];

EDIT-3:

UIActionSheet Crashes on iPad / not iPhone

http://forums.macrumors.com/showthread.php?t=997125

0
Nikolai Ruhe On

You did not yet say that you made sure self.view != nil. Seems clear that's the first thing to do. Or check when you hang in the debugger.