簡體   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