UIActionSheet Crashes on iPad / not iPhone

2019-02-11 04:53发布

问题:

When I look in the console I get this message

2010-09-18 17:04:05.284 Wasted Time[8998:207] *** Assertion failure in -[UIActionSheet showInView:], /SourceCache/UIKit_Sim/UIKit-1145.66/UIAlert.m:7073
2010-09-18 17:04:05.286 Wasted Time[8998:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: view != nil'
2010-09-18 17:04:05.286 Wasted Time[8998:207] Stack: (
    42272848,
    43430700,
    42010379,
    811796,
    3796273,
    3862560,
    9631,
    3616645,
    3688229,
    3682846,
    3690662,
    3686119,
    4983946,
    71264534,
    71263781,
    71207378,
    71206706,
    3003734,
    3030334,
    3011831,
    3043800,
    51265916,
    41552028,
    41547944,
    3002913,
    3036018,
    8314
)
terminate called after throwing an instance of 'NSException'

The code is as follows:

- (void)viewDidLoad {
    BOOL    continueYesNo;
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    continueYesNo = [prefs boolForKey:@"keyContinueMeeting"];   
    if (continueYesNo) {
        NSString *message_continue = [[NSString alloc] initWithFormat:@"Do you want to Continue the Prior Meeting"];
        UIActionSheet *actionSheet = [[UIActionSheet alloc] 
            initWithTitle:message_continue 
            delegate:self
            cancelButtonTitle:@"Reset" 
            destructiveButtonTitle:@"Continue"
            otherButtonTitles:nil];
        [actionSheet showInView:self.view];
        [actionSheet release];
        [message_continue release];
    }
}

It runs fine both in the iPhone and in the iPhone simulator, but crashes in the iPad simulator.

回答1:

The error message says:

Invalid parameter not satisfying: view != nil

Likely from this line:

[actionSheet showInView:self.view];

Since you say this works on iPhone but not iPad, that means that the code path the iPad takes to get to this line is probably different than the code path the iPhone takes to get to this line. Which means that the view controller's view property is probably not set for the iPad.

My guess: you forgot to hook up the view outlet in Interface Builder for the iPad version of the xib this view controller is using.



回答2:

I also had this problem but I was using [actionSheet showInView:[self.view window]]; (to fix another bug where only half the cancel button is selectable).

Now I conditionally change this to [actionSheet showInView:self.view]; on the iPad.

I found it's best not to the style to UIActionSheetStyleBlackTranslucent on the iPad either – the iPad has it's own style, and setting this seems to add the cancel button (which by default on the iPad is not shown).



回答3:

do like this:

[self performSelectorOnMainThread:@selector(choosePhoto) withObject:nil waitUntilDone:NO];

-(void)choosePhoto
{

    UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:@""
                                                  delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil
                                         otherButtonTitles:@"Take Photo", @"Choose from gallery", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    actionSheet.tag = 1;
    [actionSheet showInView:self.view];
    [actionSheet release];
}

Worked for me



回答4:

I believe the problem is that the view has not appeared yet. Put it in viewDidAppear: and it should work fine.



回答5:

For those like me that may be using UIActionSheet slightly differently than in the original question, but still failing on iPad, I had been using:

[actionSheet showFromRect:m_view.bounds inView:m_view animated:YES];
[actionSheet release];

...which works on iPhone but fails on iPad.

This example works in my particular app on both devices:

[actionSheet showInView:m_view];
[actionSheet release];

Regards, David



回答6:

This is an old question, but I encountered the same problem today. I have a xib file for a UIViewController that's common to iPhone and iPad, and the showInView method on the UIActionSheet was causing the iPad app to crash.

Turned out calling showInView in the viewDidAppear, instead of viewDidLoad fixed it for me. Perhaps all IB connections weren't made by the time viewDidLoad was called on iPad.

(my UIViewController was being pushed onto a UINavigationController, so I wonder if the increased animation time is what caused the whole issue_