2015-11-07 89 views
2

我有以下代碼UIAlertController斯威夫特

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) 

的對話框顯示正常,但是當我點擊一個按鈕,它不會調用處理函數。任何想法?

+0

你說的是點擊'retry'按鈕嗎? –

+0

這兩個按鈕都不起作用 – Yamila

+0

您是否嘗試添加斷點或日誌記錄以查看它是否至少到達那裏?可能聽起來很愚蠢,但這將是第一步。 –

回答

0

您可以試試這段代碼。

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 
} 
0

對於國際化,你必須:

title: "retry".localized, 

嘗試改變它像:

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

您可以爲所有要國際化的其他琴絃做同樣的,在您提交的代碼中,它們是您在前面放入「.localized」的字符串。

當然你必須在你的包中有Localizable.strings文件已經設置爲你想要的語言。如果您需要更多關於它的地方How to localize my app with Xcode 4?

祝您好運。