【问题标题】:UIActionsheet delegate clickedButtonAtIndex never gets calledUIActionsheet 委托 clickedButtonAtIndex 永远不会被调用
【发布时间】:2016-10-31 06:47:04
【问题描述】:

我正在尝试创建一个函数,该函数在从不同的视图控制器调用时创建一个操作表。 问题是,虽然创建了 uiaction 表,但我似乎无法执行任何指定的单击操作。事实上,代表永远不会被调用。 单击任何按钮时,操作表将被关闭。

我的函数代码是:

func shareAction(){
    var sheet: UIActionSheet = UIActionSheet();
    print("Calling share action!");
    let title: String = "Tell friends, make plans";

    sheet.title  = title;
    sheet.addButtonWithTitle("Cancel")
    sheet.addButtonWithTitle("WhatsApp");
    sheet.addButtonWithTitle("Facebook");
    sheet.addButtonWithTitle("Message");
    sheet.addButtonWithTitle("More");
    sheet.cancelButtonIndex = 0;
    sheet.delegate=self;
    sheet.showInView(self.view)

}

func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int)
{
    print("clicking the action sheet")
    if(buttonIndex ==0){
         print("this is 0")
}

这是在扩展 NSObject 和 UIActionSheetDelegate 的类中。 我浏览了所有文档,对导致问题的原因感到困惑。

【问题讨论】:

  • 有什么理由不使用UIAlertController?您将使用特定于您添加的每个操作的完成块,而不是委托。无论如何,UIActionSheetUIAlertView 都已被弃用,取而代之的是 UIAlertController
  • 我试图让我的代码与 iOS 7 兼容,并避免为 8 及更高版本编写不同的代码。这不是一个强有力的理由,但由于 alertview 不支持以前的版本,我更喜欢使用 uiactionsheet。
  • 支持 iOS 7 的任何具体原因?绝大多数用户已经在使用 9。如果您改变主意,Xcode 8/iOS 10 也将不再支持 iOS 7:see this link
  • 好的,注意点。但是您仍然认为我的实施有问题吗?另外,模拟器还会运行这段代码吗?
  • 我的代码库目前主要支持 iOS7。这将是一次大规模的改造,稍后会出现。我正在尝试暂时解决这个问题。

标签: ios swift ios7 uiactionsheet uiactionsheetdelegate


【解决方案1】:

我也有同样的问题。问题是处理 ActionSheet 动作并且是委托的泛型类被 ARC 释放。所以委托self 被释放,不会被调用。

确保保留实现 ActionSheet 调用的类。例如,您可以将其保存到调用视图控制器中的@property 变量中。

【讨论】:

    【解决方案2】:

    创建你的函数,让它接受一个类型为 id 的参数。您必须在其中传递当前的 viewcontroller 对象(self)。

    【讨论】:

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