将destructiveButtonTitle移动到UIActionSheet的底部

时间:2016-07-07 18:05:48

标签: swift button uiactionsheet

我希望将取消按钮移动到我的操作表的底部,成为用户可用的最后一个按钮。我有这段代码:

func openImagePicker(
    actionSheetTitle:String="Choose Image Source",
    cancelTitle:String="Cancel",
    cameraTitle:String="Camera",
    galleryTitle:String="Gallery",
    isImage:Bool=true,
    isMultipleSelection:Bool=false,
    completeHandler:(success:Bool,arrAssert:[TCAsset])->Void,
    failurer:((error:String)->(Void))?=nil)
{

    let actionSheet=UIActionSheet(title: actionSheetTitle, delegate:self, cancelButtonTitle:nil, destructiveButtonTitle:cancelTitle,otherButtonTitles: cameraTitle, galleryTitle)

    if UIDevice.currentDevice().userInterfaceIdiom == .Phone
    {
        actionSheet.showInView(UIApplication.sharedApplication().keyWindow!)
    }
    else
    {
        let appDel = UIApplication.sharedApplication().delegate as? AppDelegate


        actionSheet.showInView((appDel?.window?.rootViewController?.view)!)
    }

目前显示的动作表如下:

enter image description here

我怎样才能使取消按钮成为列表中的最后一个按钮?

谢谢:)

2 个答案:

答案 0 :(得分:1)

我没有实现你的完成块,但这应该让你开始:

func openImagePicker(
        actionSheetTitle:String="Choose Image Source",
        cancelTitle:String="Cancel",
        cameraTitle:String="Camera",
        galleryTitle:String="Gallery",
        isImage:Bool=true,
        isMultipleSelection:Bool=false,
        completeHandler:(success:Bool,arrAssert:[Bool])->Void,
        failurer:((error:String)->(Void))?=nil)
    {
        let sheet = UIAlertController(title: actionSheetTitle, message: nil, preferredStyle: .ActionSheet)
        sheet.addAction(UIAlertAction(title: cameraTitle, style: .Default, handler: { _ in
            // Code to execute when "Camera" is pressed
            print("Camera selected")
        }))
        sheet.addAction(UIAlertAction(title: galleryTitle, style: .Default, handler: { _ in
            // Code to execute when "Gallery" is pressed
            print("Gallery selected")
        }))
        sheet.addAction(UIAlertAction(title: cancelTitle, style: .Destructive, handler: { _ in
            // Code to execute when "Cancel" is pressed
            print("Cancel selected")
        }))
        presentViewController(sheet, animated: true, completion: nil)
    }

该函数必须驻留在UIViewController的实例中(因为它将调用presentViewController(...

答案 1 :(得分:1)

我觉得这是一个非常简单的答案:

您可以使用destructiveButtonIndex将destructiveButton的索引设置为您想要的位置。

{
    "details":  
        {
            "remoteAddress":"127.0.0.1",
            "sessionId":null,
            "tokenValue":"SomeLongTokenString",
            "tokenType":"Bearer",
            "decodedDetails":null
        },
    "authenticated":true,
    "userAuthentication":
        {
            "details":null,
            "authorities":
                [
                    {
                        "authority":"ROLE_ADMIN"
                    },
                    {
                        "authority":"ROLE_USER"
                    }
                ],
            "authenticated":true,
            "principal":"user",
            "credentials":"N/A",
            "name":"user"
        },
    "name":"user"
}