用按钮设置标签编号后如何将标签数据传输到新的控制器标签 [英] How to transfer label data to new controller label after label number is set with button

查看:79
本文介绍了用按钮设置标签编号后如何将标签数据传输到新的控制器标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我如何将ViewController标签:totalPar转移到ThirdViewController标签:totalPar5.我想使用减号和加号按钮传输添加到其中的数据

So how i transfer my ViewController label: totalPar to ThirdViewController label: totalPar5. And I want to transfer data that I have added there with my minus and plus buttons

这是模拟器图片,显示了我想要转移的内容

Here is simulator pic that show what I want to transfer

我的ThirdViewController几乎与ViewController相同,除了准备segue

My ThirdViewController is almost same as ViewController except prepare segue

这是我的ViewController

Here is my ViewController

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var totalPar: UILabel!
@IBOutlet weak var plusminusPar: UILabel!
@IBOutlet weak var currentPar: UILabel!


var totalPAR = Int()
var plusminusPAR = 0
var currentPAR = 0



override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}


@IBAction func minusButton(_ sender: Any) {

    totalPAR = Int(totalPar.text!)!
    self.totalPar.text = String(totalPAR - 1)

    plusminusPAR = Int(plusminusPar.text!)!
    self.plusminusPar.text = String(plusminusPAR - 1)

    currentPAR = Int(currentPar.text!)!
    self.currentPar.text = String(currentPAR - 1)
}

@IBAction func plusButton(_ sender: Any) {

    totalPAR = Int(totalPar.text!)!
    self.totalPar.text = String(totalPAR + 1)

    plusminusPAR = Int(plusminusPar.text!)!
    self.plusminusPar.text = String(plusminusPAR + 1)

    currentPAR = Int(currentPar.text!)!
    self.currentPar.text = String(currentPAR + 1)
}



@IBAction func Seuraava(_ sender: Any) {
    self.performSegue(withIdentifier: "next1", sender: Any?.self)
}


@IBAction func seiraava2(_ sender: Any) {
    self.performSegue(withIdentifier: "seuraava2", sender: Any?.self)
}



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let thirdviewcontroller = segue.destination as! ThirdViewController
    thirdviewcontroller.totalPAR5 = self.totalPAR
}


}

推荐答案

从接受的答案及其注释中,您需要开始改进代码并大致了解数据源的一些概念.

From the accepted answer and its comments you need to start improving your code and learn some concepts of data source in general.

对于您的情况,请停止不断将字符串转换为整数并转换为字符串.选择一个或另一个:

For your case stop constantly converting string to integers to string. Choose one or the other:

class ViewController: UIViewController {

var totalPAR: Int = 0
var plusminusPAR: Int = 0
var currentPAR: Int = 0

对动作和单独的方法进行自然操作:

Do natural operations on actions and separate methods:

@IBAction func minusButton(_ sender: Any) {
    addToAll(valueToAdd: -1)
    refreshLabels()
}

private func addToAll(valueToAdd: Int) {
    totalPAR += valueToAdd
    plusminusPAR += valueToAdd
    currentPAR += valueToAdd
}

private func refreshLabels() {
    self.totalPar.text = String(totalPAR)
    self.plusminusPar.text = String(plusminusPAR) 
    self.currentPar.text = String(currentPAR)
}

那么您将不会有任何问题

Then you will have no issues calling

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let thirdviewcontroller = segue.destination as? ThirdViewController {
        thirdviewcontroller.totalPAR = self.totalPAR
        thirdviewcontroller.plusminusPAR = self.plusminusPAR
        thirdviewcontroller.currentPAR = self.currentPAR
    }
}

现在,如果您在两个视图控制器中都在viewDidLoad上调用了refreshLabels,则值已成功传输.

Now if in both view controllers you call refreshLabels on viewDidLoad the values are successfully transferred.

这篇关于用按钮设置标签编号后如何将标签数据传输到新的控制器标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆