views:

163

answers:

1

I have an iPhone application which is based on the "Window based application" template and which uses a main view with some embedded subviews.

For some action I need a confirmation by the user. Therefore, I create an UIActionSheet and ask the user for feedback.

The problem is, that the action sheet does not show at all. Instead, the screen gets darker. The sheet and the requested buttons do not show. After that, the application hangs. The darkening of the screen is a normal behavior as part of the animation which normally shows the action sheet.

Curiously, the same code works fine, if invoked in the viewDidLoad method. It does not work if invoked in the buttonPressed method which starts the action requiring the confirmation.

- (void) trashButtonPressed {
   // This method is called in the button handler and (for testing
   // purposes) in the viewDidLoad method.
   NSLog( @"trashButtonPressed" );

   UIActionSheet* actionSheet =
        [[UIActionSheet alloc] initWithTitle: @"Test" 
                                    delegate: self 
                           cancelButtonTitle: @"Cancel"
                      destructiveButtonTitle: @"Delete Sheet"
                           otherButtonTitles: nil];

   [actionSheet showInView: self.view];
   [actionSheet release];
}

- (void)       willPresentActionSheet:(UIActionSheet *)  actionSheet {
   NSLog( @"willPresentActionSheet" );
}

- (void)       didPresentActionSheet:(UIActionSheet *)   actionSheet {
   NSLog( @"didPresentActionSheet" );
}

- (void)        actionSheet:(UIActionSheet *)actionSheet
  didDismissWithButtonIndex:(NSInteger)buttonIndex {
   NSLog( @"actionSheet:didDismissWithButtonIndex" );
}

As you can see, I have added some logging messages to the protocol handlers of the UIActionSheetDelegateProtocol. The "will present" and "did present" methods get called as expected, but the sheet does not show.

Does anybody know, what's wrong here?

+1  A: 

I wonder if [actionSheet showInView: self.view]; is enough to have the actionSheet have itself retained by self.view. (edit: retain count jumps from 1 to 4 so not a problem here)

Have you checked the dimensions of your view? The sheet is positioned within the view, but if self.view would refer to a big scrollview, you might just have a sheet below the surface. In short, are you sure that self.view.frame and self.view.bounds have the same values in the two situations you are referring to? Is it the same view (when just NSLog(@"%x",self.view)-ing it's address)?

edit to clarify: do

NSLog(@"%f %f %f %f",
        self.view.frame.origin.x,self.view.frame.origin.y,
        self.view.frame.size.width,self.view.frame.size.height);

and please tell what you see on the console. I get your "screen just gets darker" if I set either a 0,0,0,0 frame or a 0,0,320,800 frame, so this might be it...

mvds
That wasn't it, but it pushed me in the right direction. I solved it now.The Problem was: I accidently copied a MainWindow.xib from the iPad to the iPhone section of my universal application. The iPad version has a size of 1024x768.In "viewDidLoad", the window has not been set by the AppDelegate. Therefore, the OS does not use the wrong coordinates. Later, the window is defined and the sheet is rendered at the wrong position.Interestingly, self.view.frame has always been (0 20; 320 460), but view.window.frame was (0 0; 768 1024).Thanks.
Tobias Haustein
Just a handy tip for mvds: You can use NSStringFromCGRect(self.view.frame) instead of NSLogging out each of the frame's values individually.
Matt Rix
@Matt: thanks for the great tip!
mvds