2017-04-05 79 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()