我制作了一个简单的hello world应用程序。我希望每次点击按钮时按钮都会更改为不同的文本名称。我希望标签说'#34;你好",如果我再次点击按钮,我希望它说'再见"。我每按一下按钮就想要这个改变。我已经尝试了几天,但我仍然没有得到它。
这是代码: 导入UIKit
class ViewController: UIViewController {
@IBOutlet weak var labelText: UILabel!
@IBAction func buttonTapped(_ sender: Any) {
if labelText.text == "hello" {
} else {
labelText.text = "goodbye"
}
}
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.
}
}
答案 0 :(得分:1)
@IBAction func buttonTapped(_ sender: Any) {
if labelText.text == "hello" { //or labelText.text.equals("hello") for string comparison
labelText.text = "goodbye"
}
else {
labelText.text = "hello"
}
}