【问题标题】:UIActionSheet does not show -- the screen just gets darkerUIActionSheet 不显示——屏幕变暗了
【发布时间】:2010-08-01 00:24:09
【问题描述】:

我有一个基于“基于窗口的应用程序”模板的 iPhone 应用程序,它使用带有一些嵌入式子视图的主视图。

对于某些操作,我需要用户确认。因此,我创建了一个 UIActionSheet 并要求用户提供反馈。

问题是,操作表根本不显示。相反,屏幕变暗。工作表和请求的按钮不显示。之后,应用程序挂起。屏幕变暗是作为动画的一部分的正常行为,通常会显示操作表。

奇怪的是,如果在 viewDidLoad 方法中调用相同的代码,也可以正常工作。如果在启动需要确认的操作的 buttonPressed 方法中调用,它将不起作用。

- (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" );
}

如您所见,我已将一些日志消息添加到 UIActionSheetDelegateProtocol 的协议处理程序中。 "will present" 和"did present" 方法按预期调用,但工作表没有显示。

有谁知道,这里出了什么问题?

【问题讨论】:

  • iphone-sdk-3 标签?您实际上仍在使用 3.x SDK?

标签: iphone uiactionsheet iphone-sdk-3.0


【解决方案1】:

我想知道 [actionSheet showInView: self.view]; 是否足以让 actionSheet 本身被 self.view 保留。(编辑:保留计数从 1 跳到 4,所以不是这里有问题)

您检查过视图的尺寸吗?工作表位于视图内,但如果 self.view 引用一个大滚动视图,您可能在表面下方只有一个工作表。简而言之,您确定 self.view.frame 和 self.view.bounds 在您所指的两种情况下具有相同的值吗?是不是同一个视图(当只是NSLog(@"%x",self.view)-ing 它的地址时)?

编辑澄清:做

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);

请说出您在控制台上看到的内容。如果我设置 0,0,0,0 帧或 0,0,320,800 帧,我会得到你的“屏幕变暗”,所以这可能是它......

【讨论】:

  • 不是这样,但它把我推向了正确的方向。我现在解决了。问题是:我不小心将一个 MainWindow.xib 从 iPad 复制到了我的通用应用程序的 iPhone 部分。 iPad版本的尺寸为1024x768。在“viewDidLoad”中,窗口还没有被 AppDelegate 设置。因此,操作系统不会使用错误的坐标。后来,窗口被定义,工作表被渲染在错误的位置。有趣的是,self.view.frame 一直是 (0 20; 320 460),但 view.window.frame 是 (0 0; 768 1024)。谢谢。
  • 对 mvds 的一个方便提示:您可以使用 NSStringFromCGRect(self.view.frame) 而不是 NSLogging 单独注销每个帧的值。
  • @Tobias:我也有同样的问题。我的代码在 iOS3.2 及更高版本上运行良好,但会导致您在早期版本中描述的行为。你是如何设置正确的窗口大小的?我收到只读属性错误。谢谢。
  • 我只是将错误的 XIB(来自 iPad)替换为 iPhone 的 XIB。新的 XIB 包含正确的窗口尺寸并被操作系统正确使用。我没有手动/代码更改窗口尺寸。
【解决方案2】:

实际上,我认为您遇到了与我看到的相同的问题。

在 iOS 8 和 9 中,UIActionSheet确实会显示...哦...但是在屏幕键盘后面,所以你看不到它.你只是看到屏幕的其余部分变暗了。

这显然是 Apple 最新的 UI 改进理念,旨在保持屏幕整洁。 ;-)

简单的解决方案是在显示您的UIActionSheet之前添加一行代码:

[self.view endEditing:true];

这会关闭屏幕键盘,让您漂亮的操作表再次可见。

或者,我有 documented here 如何将您的 UIActionSheet 替换为 UIAlertController。

【讨论】:

    猜你喜欢
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 1970-01-01
    相关资源
    最近更新 更多