[英]How can I increment a label by pressing a button that is in a separate view?
so basically I'm familiar with app development and Xcode, but I'm still in the beginning stages of learning and I need some help. 因此,基本上我对应用程序开发和Xcode熟悉,但是我仍处于学习的起步阶段,需要一些帮助。 I've created a emotions quiz type UI and at the end of the 10 questions, I have a page that displays the different emotions and the scores that the user earned throughout the quiz.
我创建了一个情绪测验类型UI,在10个问题的末尾,我有一个页面显示不同的情绪以及用户在整个测验中获得的分数。 What I need help on is wen the user clicks a button on question 1 to get the label on my results page to increase by 1. I've tried many different methods so far but can't seem to quite figure it out.
我需要帮助的是,用户单击问题1上的按钮以使结果页面上的标签增加1。到目前为止,我已经尝试了许多不同的方法,但似乎不太清楚。 Ive linked my buttons to their respective view controller as actions and the labels to their respective view controller as outlets.
我已经将我的按钮作为动作链接到它们各自的视图控制器,并将标签作为插座链接到它们各自的视图控制器。
import UIKit
class Question1: UIViewController {
@IBAction func buttonlightgreen(_ sender: UIButton) {
print ("light green")
sadscoreText += 1
}
@IBAction func buttonred(_ sender: Any) {
print ("red")
angryscoreText += 2
happyscoreText += 1
}
@IBAction func buttonpurple(_ sender: Any) {
print ("purple")
annoyedscoreText += 1
stressedscoreText += 1
}
@IBAction func buttondarkgreen(_ sender: Any) {
print ("dark green")
relaxedscoreText += 2
tiredscoreText += 1
}
@IBAction func buttonyellow(_ sender: Any) {
print ("yellow")
happyscoreText += 1
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
That is the code for the buttons and this is the code for the labels. 这是按钮的代码,这是标签的代码。
import UIKit
var sadscoreText = 0
var happyscoreText = 0
var relaxedscoreText = 0
var angryscoreText = 0
var annoyedscoreText = 0
var tiredscoreText = 0
var stressedscoreText = 0
class Results: UIViewController {
@IBOutlet weak var sadscore: UILabel!
@IBOutlet weak var happyscore: UILabel!
@IBOutlet weak var relaxedscore: UILabel!
@IBOutlet weak var angryscore: UILabel!
@IBOutlet weak var annoyedscore: UILabel!
@IBOutlet weak var tiredscore: UILabel!
@IBOutlet weak var stressedscore: UILabel!
var sadscoreText: Int = 0 {
didSet {
sadscore.text = "\(sadscoreText)"
}
}
override func viewDidLoad() {
super.viewDidLoad()
sadscoreText = 0
happyscoreText = 0
relaxedscoreText = 0
angryscoreText = 0
annoyedscoreText = 0
tiredscoreText = 0
stressedscoreText = 0
}
}
I believe the reason its not working is because you are assigning 0 to the values before assigning the modified values to label in viewDidLoad method. 我相信它无法正常工作的原因是,在将修改后的值分配给viewDidLoad方法中的标签之前,您正在为这些值分配0。 You can achieve the required behaviour like this:
您可以实现所需的行为,如下所示:
Modify your Results view controller as follows: 修改您的结果视图控制器,如下所示:
import UIKit
var sadscoreText = 0
var happyscoreText = 0
var relaxedscoreText = 0
var angryscoreText = 0
var annoyedscoreText = 0
var tiredscoreText = 0
var stressedscoreText = 0
class Results: UIViewController {
@IBOutlet weak var sadscore: UILabel!
@IBOutlet weak var happyscore: UILabel!
@IBOutlet weak var relaxedscore: UILabel!
@IBOutlet weak var angryscore: UILabel!
@IBOutlet weak var annoyedscore: UILabel!
@IBOutlet weak var tiredscore: UILabel!
@IBOutlet weak var stressedscore: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
sadscore.text = "\(sadscoreText)"
happyscore.text = "\(happyscoreText)"
//follow the same for other labels
//you can set the values again to zero after showing the labels on the screen like this
sadscoreText = 0
happyscoreText = 0
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.