UILabel没有更新但继承了价值

时间:2017-04-05 14:52:05

标签: class methods swift3 singleton uilabel

我正在尝试更新在我的ViewController中设置的UILabel。更新来自于我的collectionView中的一个单元格,这是另一个类。标签继承了该值,但没有以图形方式更新。我怀疑这是因为按下单元格时会创建一个新的视图控制器实例,而不会更新当前的实例。

如何更新当前实例的UIlabel?

的ViewController:

import UIKit
import CoreData

class mainHomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    override func viewDidLoad() {
        super.viewDidLoad()


        setupBasketBar()

    }

    let totNumber: UILabel = {
        let label = UILabel()
        label.text = "0"
        label.numberOfLines = 2
        return label
    }()

    func updateBarText() {
        totNumber.text = "Update"
    }

    func setupBasketBar() {

        self.view.addSubview(totNumber)
        totNumber.translatesAutoresizingMaskIntoConstraints = false
        totNumber.leadingAnchor.constraint(equalTo: view.leadingAnchor,constant: 330).isActive = true
        totNumber.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        totNumber.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: 0).isActive = true
        totNumber.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 5).isActive = true


    }

}

触发操作的函数( collectionView类):

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {


    mainHomeController().updateBarText()

}

1 个答案:

答案 0 :(得分:0)

假设您已在collection​View:​did​Select​Item​At​Index​Path:​中实施mainHomeController,则可以将mainHomeController().updateBarText()替换为updateBarText()