2017-04-05 41 views
0

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

如何更新我当前实例的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类):

回答

0

假设你已经在mainHomeController实施collection​View:​did​Select​Item​At​Index​Path:​

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


    mainHomeController().updateBarText() 

},你可以代替mainHomeController().updateBarText()updateBarText()