所以我对编码非常陌生,我正在上课,学习如何在xcode中创建应用程序。我们被分配从头开始制作我们的第一个应用程序。我的想法是制作一个猜谜游戏,用户近距离拍照,他们必须猜测它是什么。我希望它在标签,文本字段和按钮的位置。他们在文本字段中键入他们的猜测,单击按钮检查他们的答案,然后标签文本更改说"你错了"或者你是对的"。我已经看了很多,并且根据在文本字段中输入的内容,找不到更改标签文本的方法。我假设我会使用if-else语句,但似乎只适用于整数。有什么帮助吗?我也连接了所有东西。非常感谢提前!
答案 0 :(得分:1)
你去了:
import UIKit
import WebKit
class ViewController: UIViewController{
var dictionaryForQuestionAndAnswer = ["what is the best way to learn coding?":"trying","who is best person to ask question?":"Google"]
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var textField: UITextField!
var answerIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
questionLabel.text = ""
}
@IBAction func questionClicked(_ sender: Any) {
let random = randomInt(min: 1, max: 2)
if(random == 1)
{
answerIndex = 1
questionLabel.text = "what is the best way to learn coding?"
}
else
{
answerIndex = 2
questionLabel.text = "who is best person to ask question?"
}
}
@IBAction func answerClicked(_ sender: Any) {
if let answer = textField.text
{
if( answerIndex == 1 && answer == dictionaryForQuestionAndAnswer["what is the best way to learn coding?"])
{
questionLabel.text = "You are correct"
}
else if( answerIndex == 2 && answer == dictionaryForQuestionAndAnswer["who is best person to ask question?"])
{
questionLabel.text = "You are correct"
}
else
{
questionLabel.text = "You are wrong"
}
}
}
func randomInt(min: Int, max:Int) -> Int {
return min + Int(arc4random_uniform(UInt32(max - min + 1)))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
输出