繁体   English   中英

swift中的UIActionSheet将取消按钮放在iOS 7中

[英]UIActionSheet in swift puts Cancel button top in iOS 7

我正在将我的应用程序从目标c重写为Swift,我注意到UIActionSheet在Swift版本中的行为与在obj-c版本中的行为不同。

对象版本

在此输入图像描述

Swift版本

在此输入图像描述

这只是iOS 7上的一个问题,它在iOS 8上运行正常(意味着取消在底部),适用于Swift和Obj-c版本

这是相关的一段代码:

var sheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil)
sheet.addButtonWithTitle("Camera")
sheet.addButtonWithTitle("Photo Library")
sheet.showInView(self.controller.view)

知道怎么解决吗?

事实证明,这是在提出问题后立即找到答案的另一个案例。

我所要做的只是添加取消按钮作为另一个按钮,然后指定其索引:

var sheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil)
sheet.addButtonWithTitle("Camera")
sheet.addButtonWithTitle("Photo Library")
sheet.addButtonWithTitle("Cancel")
sheet.cancelButtonIndex = 2
sheet.showInView(self.controller.view)

不确定他们是否改变了UIActionSheet在Swift中应该如何工作的方式,或者是否是因为它在iOS 8中被弃用而无法修复的错误

这是我在viewDidLoad()或按钮操作上得到的代码,如下所示:

let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done", otherButtonTitles: "Yes", "No")
          actionSheet.showInView(self.view)

然后为他们的动作添加另一个函数,如下所示

func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int)
    {
        switch buttonIndex{

        case 0:
            NSLog("Done");
            break;
        case 1:
            NSLog("Cancel");
            break;
        case 2:
            NSLog("Yes");
            break;
        case 3:
            NSLog("No");
            break;
        default:
            NSLog("Default");
            break;
            //Some code here..

        }

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM