简体   繁体   English

如何通过按单独视图中的按钮来增加标签?

[英]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.

相关问题 在主视图中按下按钮时,如何在弹出视图 controller 中更改 label 的 label 文本? - How to change label text of a label in a popover view controller when pressing a button in the main view? Swift:由于按下按钮,如何将来自三个单独文本字段的数据发布到自定义单元格的标签上? - Swift: How can I post data from three separate text fields onto labels in a custom cell, as the result of pressing a button? 如何在Swift中按下按钮来增加标签值 - How do I increment a label value with a button press in Swift 按下按钮会将文本发送到单独的表视图控制器中的单元格 - Pressing a button sends text to a cell in a separate table view controller 如何通过按按钮将人像更改为横向和横向? - How can i change portrait to landscape and back by pressing a button? 如何通过按下按钮添加容器小部件? - How can I add a container widget by pressing the button? 单击按钮时如何增加标签值 - How to increment a label value when click a button 单击下一个视图控制器的按钮时,如何显示加载标签? - How can I get my loading label to show up when I click button to next view controller? 按下按钮后如何修改视图控制器? - How to modify view controller after pressing a button? 如何在自定义UITableViewCell中按下按钮后更改标签的文本 - How to change the text of a label after pressing the button which are in Custom UITableViewCell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM