【问题标题】:UIAlertController SwiftUIAlertController 斯威夫特
【发布时间】:2016-02-08 19:13:27
【问题描述】:

我有以下代码

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)

对话框显示正常,但是当我单击按钮时,它不会调用处理函数。有什么想法吗?

【问题讨论】:

  • 你是说点击按钮retry
  • 两个按钮都不起作用
  • 将处理程序的主体替换为简单的 NSLogs 后,此代码对我有用。调用处理程序并隐藏操作表。您可以在处理程序中放置断点以确保它们不会被调用吗?当您按下这些按钮时,操作表会消失吗?也许问题出在其他地方。
  • 您是否尝试过添加断点或记录以查看它是否至少到达那里?听起来可能很愚蠢,但这将是第一步。

标签: ios swift uialertcontroller


【解决方案1】:

对于您所拥有的国际化:

title: "retry".localized,

尝试将其更改为:

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

您可以对要国际化的所有其他字符串执行相同的操作,在您提供的代码中,它们是您将“.localized”放在前面的字符串。

当然,您必须在您的捆绑包中将 Localizable.strings 文件设置为您想要的语言。如果您需要更多信息,请访问How to localize my app with Xcode 4?

祝你好运。

【讨论】:

    【解决方案2】:

    你可以试试这个代码。

    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 
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-22
      • 2017-03-24
      • 1970-01-01
      • 2017-11-13
      • 2015-08-19
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多