I have a UIViewController and in it a UIToolbar . They get a copy from the storyboard.
I created my own class for UIToolbar . Based on some logic, I make or donβt show buttons on it.
UIViewController needs to perform an action when some of the buttons are enabled. To do this, I created a delegate protocol in UIToolbar .
Currently, when I decline a view, it is stored in memory. Further research showed that my delegate created a conservation cycle.
In Objective-C, we simply define delegates as weak . However, I use Swift, and this does not allow me to define the delegate variable as weak :
weak var navigationDelegate: MainToolBarDelegate? // 'weak' cannot be applied to non-class type 'MainToolBarDelegate'
When I reject the view controller, I set self.toolBar.navigationDelegate = nil and the memory is cleared. But this is wrong!
Why am I getting a save loop and why can't I just define the delegate as weak ?
ios swift delegates retain-cycle
Bocaxica
source share