簡體   English   中英

UIAlertController迅捷

[英]UIAlertController Swift

我有以下代碼

let alertController = UIAlertController(title: "error_title".localized, message: "error".localized, preferredStyle: .ActionSheet)

    let retryAction = UIAlertAction(
        title: "retry".localized,
        style: .Default,
        handler: {
            (action:UIAlertAction!) in
            self.fetch()
        }
    )
    alertController.addAction(retryAction)

    let cancelAction = UIAlertAction(
        title: "cancel".localized,
        style: .Default,
        handler: {
            (action:UIAlertAction!) in
            self.navigationController!.popViewControllerAnimated(true)
        }
    )
    alertController.addAction(cancelAction)

    self.presentViewController(alertController, animated: true, completion: nil)

對話框顯示為正常,但是當我單擊按鈕時它不會調用處理程序函數。 任何想法?

您可以嘗試此代碼。

let alertController = UIAlertController(title: "AlertCotrol", message: "A standard alert.", preferredStyle: .Alert)

let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ... Do something 
}
alertController.addAction(cancelAction)

let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
// ... Do something 
}
alertController.addAction(OKAction)

self.presentViewController(alertController, animated: true) {
// ... Do something 
}

為了實現國際化,您需要:

title: "retry".localized,

嘗試將其更改為:

title: (NSLocalizedString("retry" , comment: "Retry something.") as String),

您可以對要國際化的所有其他字符串執行相同的操作,在您提供的代碼中,它們就是您將“ .localized ”放在前面的字符串。

當然,您的捆綁包中必須已將Localizable.strings文件設置為所需的語言。 如果需要,請參見如何使用Xcode 4本地化我的應用程序?

祝好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM