gpt4 book ai didi

ios - 应用程序不在标签上显示 var 字符串

转载 作者:行者123 更新时间:2023-11-30 12:56:26 25 4
gpt4 key购买 nike

我正在尝试制作一个应用程序,它可以获取学生姓名和总分(满分 1000 分),并在按下按钮时将其显示在标签上。显示的公式是“x 学生获得 y 成绩”。由于某种原因(我不确定)我已经连接了 Storyboard并且代码看起来是正确的;标签不显示字符串。相反,它给了我类似的东西:

可选(UITextField:0x7ff6696095550;框架=”等,等等

如果有人可以查看我的代码并帮助我如何让该字符串显示在标签上 - 我将非常感激。

    import UIKit

class ViewController: UIViewController {

@IBOutlet weak var gradeTF: UITextField!
@IBOutlet weak var nameGR: UITextField!
@IBOutlet var label1 : UILabel!


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}



@IBAction func submitTapped(_ sender: UIButton) {

var gradeMS = ""

if (gradeTF.text?.isEmpty)! {

gradeMS = "Please enter point total"

}
else {
let grade : Int = Int(gradeTF.text!)!


if grade > 1000 {
gradeMS = "ERROR - Invalid point total"
label1.text = gradeMS
}
else if grade > 900 && grade <= 1000 {
gradeMS = "\(nameGR)'s grade is \"A\""
label1.text = gradeMS
}
else if grade > 800 && grade <= 900 {
gradeMS = "\(nameGR)'s grade is \"B\""
label1.text = gradeMS
}
else if grade > 700 && grade <= 800 {
gradeMS = "\(nameGR)'s grade is \"C\""
label1.text = gradeMS
}
else if grade > 600 && grade <= 700 {
gradeMS = "\(nameGR)'s grade is \"D\""
label1.text = gradeMS
}
else {
gradeMS = "\(nameGR)'s grade is \"F\""
label1.text = gradeMS
}
}


func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

}

最佳答案

您应该使用 nameGR 文本字段中的文本,而不是文本字段本身。

替换

gradeMS = "\(nameGR)'s grade is \"A\""

gradeMS = "\(nameGR.text!)'s grade is \"A\""

submitTapped IBAction 中的所有位置。

@IBAction func submitTapped(_ sender: UIButton) {

var gradeMS = ""
let name = nameGR.text

if (gradeTF.text?.isEmpty)! {

gradeMS = "Please enter point total"

}
else {
let grade : Int = Int(gradeTF.text!)!


if grade > 1000 {
gradeMS = "ERROR - Invalid point total"
label1.text = gradeMS
}
else if grade > 900 && grade <= 1000 {
gradeMS = name! + "'s grade is \"A\""
label1.text = gradeMS
}
else if grade > 800 && grade <= 900 {
gradeMS = name! + "'s grade is \"B\""
label1.text = gradeMS
}
else if grade > 700 && grade <= 800 {
gradeMS = name! + "'s grade is \"C\""
label1.text = gradeMS
}
else if grade > 600 && grade <= 700 {
gradeMS = name! + "'s grade is \"D\""
label1.text = gradeMS
}
else {
gradeMS = name! + "'s grade is \"F\""
label1.text = gradeMS
}
}
}

关于ios - 应用程序不在标签上显示 var 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40346967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com