【问题标题】:How to display custom view in UIActionSheet?如何在 UIActionSheet 中显示自定义视图?
【发布时间】:2010-04-11 04:31:05
【问题描述】:

我有一个带有日期选择器的 UIView,我想在操作表中显示它。我正在使用以下代码:

-(IBAction) button_click:(id)sender{
    //UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"the title" delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Destructive" otherButtonTitles:@"other", nil];

    UIActionSheet *sheet = [[UIActionSheet alloc] init];
    ActionSheetController *actionSheet = [[ActionSheetController alloc]  initWithNibName:@"ActionSheetView" bundle:nil];    
    [sheet addSubview:actionSheet.view];

    [sheet showInView:self.view];
}

我得到的是新视图的顶部部分从底部升起,仅此而已。如果我注释中间两行代码并取消注释顶部以显示常规操作表,它工作正常。有什么想法我可能做错了吗?

【问题讨论】:

  • ActionSheetController 类是你自己的吗?可以分享一下代码吗?

标签: iphone cocoa-touch uiactionsheet


【解决方案1】:

使用 UIActionSheetDelegate 之后,您可以使用此委托将图像添加到 alertsheet 的按钮

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet{

   UIImageView *sample = [[UIImageView alloc] initWithImage:[UIImage       
   imageNamed:@"Someimage.png"]];
   [actionSheet addSubView:sample];

}

【讨论】:

  • 如果正确的话那就太好了。失败:“'UIActionSheet' 没有可见的@interface 声明选择器'addSubView:'”
  • 只是小写 v..its [actionSheet addSubview:sample];
  • 你可能要小心这个。苹果可能不喜欢它。从文档中:“UIActionSheet 不是为子类而设计的,您也不应该将视图添加到其层次结构中。如果您需要提供比 UIActionSheet API 提供的更多自定义的工作表,您可以创建您的拥有并以模式呈现它 presentViewController:animated:completion:."
【解决方案2】:

我不相信您可以在 UIActionSheet 中指定自定义视图。我的理解是,您只能显示一个标题和 1 个或多个按钮。以下是苹果文档中对 UIActionSheet 的描述:

使用 UIActionSheet 类为用户提供一组备选方案,以了解如何继续执行给定任务。您还可以使用操作表来提示用户确认潜在危险的操作。操作表包含一个可选的标题和一个或多个按钮,每个按钮对应一个要采取的操作。

UIActionSheet Class Reference

【讨论】:

  • 如果你看一下这篇 stackoverflow.com/questions/2608239/… 的帖子,我有一个 zipcar 应用程序的链接,它似乎使用了自定义操作表。
  • 毕竟 UIActionSheet 只是另一种类型的 UIView,从技术上讲,您尝试做的事情是可能的。我担心的是,当您将应用程序提交给 Apple 时,您可能会遇到问题,因为他们希望 UIActionSheet 以非常特定的方式使用,如 iPhone HIG developer.apple.com/iphone/library/documentation/UserExperience/… 中所述,您是否考虑过创建自定义模式视图而不是尝试使用 UIActionSheet?
  • 模态视图控制器不会隐藏/禁用导航栏吗?
  • 是的,但 UIActionSheet 也是如此。导航栏位于半透明覆盖层后面。为什么要在显示 UIActionSheet 时访问导航栏?它专门设计用于强制用户选择操作或关闭视图。你想用需要嵌入自定义视图的 UIActionSheet 来实现什么?如果我有更多详细信息,我可能会给出更详细的答复。
  • 看看我上面关于 zipcar 应用程序的第一条评论。这就是我想要做的。
【解决方案3】:

我发现出现这种行为是因为我没有正确使用 UIActionSheet 构造函数。应包括按钮和标题:

UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Date Picker" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

【讨论】:

    【解决方案4】:

    您可能想查看我制作的 UIActionSheet 替代方案,它可以让您显示自定义内容视图而没有任何肮脏的黑客攻击:https://github.com/JonasGessner/JGActionSheet 它与 UIActionSheet 相比还有一些其他优势!

    【讨论】:

      猜你喜欢
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多