为啥我的一个 segues 不起作用?
Posted
技术标签:
【中文标题】为啥我的一个 segues 不起作用?【英文标题】:How come one of my segues isn't working?为什么我的一个 segues 不起作用? 【发布时间】:2017-07-29 07:27:00 【问题描述】:我正在开发一个测验应用程序,当您正确回答问题时,它会转到一个视图控制器,如果您按错误答案,它会转到另一个视图控制器。但是,当我运行模拟器时,如果我点击了错误的答案,除了倒数计时器重置之外,什么都不会发生。但是,如果我单击正确的答案,那么 segues 就可以了。这是为什么呢?
这是我的代码:
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)
【问题讨论】:
提供您的情节提要的图像以显示转场 @DharmeshKheni 我如何在这里分享项目? 【参考方案1】:您在 buttonAction 中输入了错误的条件,我现在已更正它检查。只需替换 else if 条件
@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
【讨论】:
以上是关于为啥我的一个 segues 不起作用?的主要内容,如果未能解决你的问题,请参考以下文章