【发布时间】:2017-02-17 01:06:42
【问题描述】:
已经提出了这样的问题,但我尝试了这些建议,但它仍然对我不起作用。 :/
我正在尝试使用本教程制作一个测验应用程序:https://www.youtube.com/watch?v=KNAQ3Y8PGkM&t=1s
这是一个下载我的项目的链接:https://www.dropbox.com/s/bwvg6fyrrzudued/kat%20quiz.zip?dl=0
一切都很完美,除了我收到错误:
无法将“UIView”(0x10d54a4c0)类型的值转换为“UIButton”(0x10d552120)
非常感谢一些帮助。我在我的知识和搜索中尝试了一切,谢谢。 :)
import UIKit
class ViewController: UIViewController {
let questions = ["What color is Courage the Cowardly Dog?", "What is the name of the woman that lives with Courage?", "What is the name of the man that lives with Courage?"]
let answers = [["Purple","Grey","Orange"],["Muriel", "Angela", "Elizabeth"], ["Eustace", "Robert", "Ezio"]]
//Variables
var currentQuestion = 0
var rightAnswerPlacement:UInt32 = 0
var points = 0
//Label
@IBOutlet weak var lbl: UILabel!
//Button
@IBAction func action(_ sender: AnyObject) {
if (sender.tag == Int(rightAnswerPlacement))
{
print ("RIGHT!")
points += 1
}
else
{
print ("WRONG!!!!!!")
}
if (currentQuestion != questions.count)
{
newQuestion()
}
else{
performSegue(withIdentifier: "showScore", sender: self)
}
}
override func viewDidAppear(_ animated: Bool) {
newQuestion()
}
//Function that displays new question
func newQuestion(){
lbl.text = questions[currentQuestion]
rightAnswerPlacement = arc4random_uniform(3)+1
//Create a button
var button:UIButton = UIButton()
var x = 1
for i in 1...3
{
//Create a button
button = view.viewWithTag(i) as! UIButton
if (i == Int(rightAnswerPlacement))
{
button.setTitle(answers[currentQuestion][0], for: .normal)
}
else
{
button.setTitle(answers[currentQuestion][x], for: .normal)
x = 2
}
}
currentQuestion += 1
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
【问题讨论】:
-
你能澄清一下吗?我对编程非常陌生。谢谢:)
标签: ios iphone swift uiview uibutton