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