Swift 为什么我的一个序列不是';不行吗?
我正在开发一个测验应用程序,当你正确回答一个问题时,它会转到一个视图控制器,如果你按错了答案,它会转到另一个视图控制器。然而,当我运行模拟器时,如果我点击一个错误的答案,除了倒数计时器重置外,什么也不会发生。然而,如果我点击正确的答案,那么segues就可以正常工作。为什么会这样 这是我的密码:Swift 为什么我的一个序列不是';不行吗?,swift,function,segue,viewcontroller,Swift,Function,Segue,Viewcontroller,我正在开发一个测验应用程序,当你正确回答一个问题时,它会转到一个视图控制器,如果你按错了答案,它会转到另一个视图控制器。然而,当我运行模拟器时,如果我点击一个错误的答案,除了倒数计时器重置外,什么也不会发生。然而,如果我点击正确的答案,那么segues就可以正常工作。为什么会这样 这是我的密码: import UIKit class ViewController: UIViewController { var questionList = [String]() var cou
import UIKit
class ViewController: UIViewController {
var questionList = [String]()
var counter = 15
var timer = Timer()
var rightAnswerBox:UInt32 = 0
var index = 0
//Question Label
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var questionTimer: UILabel!
func updateCounter() {
counter -= 1
questionTimer.text = String(counter)
if counter == 0 {
timer.invalidate()
wrongSeg()
}
}
func randomQuestion() {
//random question
if questionList.isEmpty {
questionList = Array(QADictionary.keys)
}
let rand = Int(arc4random_uniform(UInt32(questionList.count)))
questionLabel.text = questionList[rand]
//matching answer values to go with question keys
var choices = QADictionary[questionList[rand]]!
questionList.remove(at: rand)
//create button
var button:UIButton = UIButton()
//variables
var x = 1
rightAnswerBox = arc4random_uniform(4)+1
for index in 1...4
{
button = view.viewWithTag(index) as! UIButton
if (index == Int(rightAnswerBox))
{
button.setTitle(choices[0], for: .normal)
}
else {
button.setTitle(choices[x], for: .normal)
x += 1
}
}
randomImage()
}
//dictionary filled with question keys and answer values
let QADictionary = [:]
//wrong view segue
func wrongSeg() {
performSegue(withIdentifier: "incorrectSeg", sender: self)
}
//proceed screen
func rightSeg() {
performSegue(withIdentifier: "correctSeg", sender: self)
}
//Answer Button
@IBAction func buttonAction(_ sender: AnyObject) {
if (sender.tag == Int(rightAnswerBox)) {
rightSeg()
timer.invalidate()
print ("Correct!")
}
if counter != 0 {
counter = 15
} else if (sender.tag != Int(rightAnswerBox)) {
wrongSeg()
print ("Wrong!")
timer.invalidate()
questionList = []
}
}
override func viewDidAppear(_ animated: Bool) {
randomQuestion()
questionTimer.text = String(counter)
timer = Timer.scheduledTimer(timeInterval: 1, target:self, selector: #selector(ViewController.updateCounter), userInfo: nil, repeats: true)
}
}
你把错误的条件放在按钮里了,我现在已经纠正了。如果条件允许,只需更换其他部件即可
@IBAction func buttonAction(_ sender: AnyObject) {
if (sender.tag == Int(rightAnswerBox)) {
rightSeg()
timer.invalidate()
print ("Correct!")
}/*Here is change*/
else if (sender.tag != Int(rightAnswerBox))
{
wrongSeg()
print ("Wrong!")
timer.invalidate()
questionList = []
}
if counter != 0 {
counter = 15
}
}
提供故事板的图像,显示segue@DharmeshKheni如何在此处共享项目?