有一个类似这样的问题,但我尝试了这些建议,但它仍然不适用于我。 :/无法将类型'UIView'(0x10d54a4c0)的值转换为'UIButton'(0x10d552120)
我尝试使用本教程做一个测验应用程序:https://www.youtube.com/watch?v=KNAQ3Y8PGkM&t=1s
下面是一个链接,下载我的项目:https://www.dropbox.com/s/bwvg6fyrrzudued/kat%20quiz.zip?dl=0
一切都是完美的,除了我的错误:
Could not cast value of type 'UIView' (0x10d54a4c0) to '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.
}
}
请问您能澄清一下吗?我对编程超新颖。谢谢:) –