【问题标题】:My UIBarButtonItem Code not executing, but everything is linked up properly我的 UIBarButtonItem 代码未执行,但所有内容都已正确链接
【发布时间】:2020-12-07 03:32:48
【问题描述】:

这是一个相当简单却又令人费解的问题。我有一个 UIBarButtonItem:

@IBAction func doneButtonPressed(_ sender: UIBarButtonItem) {
    guard let name = nameTextField.text,
               let estimatedHeight = estimatedHeightTextField.text,
               let locationDescription = descripitionTextView.text,
               let warnings = warningsTextView.text,
               let image = jumpSpotImageView.image else {
                   // notify user requred fields were blank
                   return
           }
           delegate?.createJumpSpotAnnotation(name: name, estimatedHeight: estimatedHeight, locationDescription: locationDescription, warnings: warnings, image: image)
           print("doneButton Pressed")
           dismiss(animated: true, completion: nil)
}

然而,代码完全无关紧要。无论我尝试什么,一旦单击按钮,代码都不会执行。我把storyboard中实际的BarButtonItem和代码的连接重做了几次,全部删除重新粘贴,重新启动Xcode等等。IBAction旁边的圆圈已经满了,我什至放了一个print语句在函数中确保我的代码不只是错误的。然而,打印语句甚至没有出现在调试器中,告诉我代码根本没有执行。知道有什么问题吗?这太奇怪了。

【问题讨论】:

  • 您是否尝试将 print 语句放在 guard let 语句之前(或在第一行设置断点)以确保实际调用了该方法?可能是您的 guard let 中的一条语句失败了
  • @Malik 就是这样。我不敢相信我没有意识到这一点。谢谢大声笑
  • 一个新的视角总是有帮助的哈哈

标签: ios swift xcode button uibarbuttonitem


【解决方案1】:

因为有一个guard let,它确保名称、estimatedHeight、locationDescription、warnings 和 image 存在。如果其中一个为 nil,则永远不会执行以下代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多