ios - 消息 : "Fail! Play Again" does not display when score < 3

标签 ios swift alert

我是 iOS-Swift 的新手,在 score < 3 时不显示消息:“失败!再玩一次” .

func discussScore(sender: AnyObject){       
  // after asking all questions, if score >=20, the player accesses stage 2
    if (questionField.text == listOfQuestions[4]) {
          if (score >= 3) {
              //go to next step
          }

          if (score < 3) {
              alertMessageWrong("Fail! Play Again") // (here's my problem)
          }                    
     }
}

func alertMessageWrong(theMessage2: String) {

    let alertController = UIAlertController(title: " ", message:theMessage2, preferredStyle: UIAlertControllerStyle.Alert)
    alertController.view.tintColor = UIColor.redColor()
    alertController.addAction(UIAlertAction(title:theMessage2, style: UIAlertActionStyle.Default,handler: nil))
    self.presentViewController(alertController, animated: true, completion: nil)

}

最佳答案

首先,我会在这一行放置一个断点:

if (questionField.text == listOfQuestions[4])

并逐步查看每一行以查看采用了哪条路线...您可以了解如何调试 here .

要检查您是否回答了正确的问题,最好使用“步长”整数而不是比较字符串。在回答每个问题后增加此整数。然后你可以检查你是否在做最后一个问题:

if currentStep >= listOfQuestions.count { ... }

其次,如果您这样做,代码会更简洁:

let minimumScore = 3
if score < minimumScore { 
    showAlert()
    return
}
// go to next step.

关于ios - 消息 : "Fail! Play Again" does not display when score < 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35749903/

相关文章:

ios - 选择要在 didEndDisplaying 中使用的单元格类

javascript - If 语句未触发警报 - 表格/测验

kubernetes - K8s-节点警报

ios - 在 NavBar( Storyboard)ios 上向 UIBarButtonItem 添加操作功能

ios - 页面 View 导航栏标题

ios - 某些 iOS 框架如何使用不带 @objc 的可选来定义 Swift 协议(protocol)?

javascript - 如何在 React Native 中使用自定义警报?

ios - 我的 UICollectionView 中没有 ItemSelected 调用?

ios - Nativescript - ActionBar 错误

swift - 做声明