Indra Sen
Indra Sen

Reputation: 37

How to accès iboutlet from another class in swift

so I want to change the button colour from view controller but the problem is my button referencing to my collection view class. I dunno how to call the button from another class

collection view

class CollectionViewCell: UICollectionViewCell {

@IBOutlet weak var menusButton: UIButton!

public func MenuButtonFunction (){
menusButton.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
}

Viewcontroller

var menuButtonFunc = CollectionViewCell()
override func viewDidLoad() {
    super.viewDidLoad()
menuButtonFunc.MenuButtonFunction()

     doneButton.layer.cornerRadius = 10

}

Upvotes: 1

Views: 50

Answers (2)

vitalyster
vitalyster

Reputation: 5266

You should not instantiate your cell with constructor call - it will not create child items and that is the reason why you have nil reference on your button. You should call dequeueReusableCell UICollectionView method to obtain fully created cell.

Upvotes: 0

10623169
10623169

Reputation: 1044

My first instinct would be to use delegation or the observer pattern.

I can't say much more without knowing more detail of your code (i.e. if it really is simple, view-hierarchy-wise, then Joakim Danielson's comment of just using a public function is probably your best solution).

Upvotes: 1

Related Questions