I thinks it is not a good idea to know inside the view about parent structures. It is breaks encapsulation and leads to hard maintenance, bugs and extra relations.
As a simple way you can define function addTarget for your custom UIView, that will work like a bridge.
class UICustomView {
func addTarget(target: AnyObject, action: Selector, forControlEvents: UIControlEvents) {
menuControlButton.addTarget(target, action: action, forControlEvents: forControlEvents)
}
func setupViews() {...}
func toggleButton(sender: MenuControlButton!) {...}
class SomeOtherViewController {
func someInitFunc {
let viewWithButton = UICustomView()
viewWithButton.addTarget(self, "foo:", .TouchUpInside)
}
func foo(sender: AnyObject) {
let isActive = sender.toggleActive() // switches button image and returns whether active or not after
NSUserDefaults.standardUserDefaults().setBool(isActive, forKey: sender.title)
self.reloadCalendar()
}
}
Then your controller only knows that UICustomView can handle events and UICustomView have know nothing about SomeOtherViewController
If you have more than one button inside you custom view perhaps it would be a good idea to implement something like UIAlertController with type UIAlertControllerStyle.ActionSheet
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…