简体   繁体   English

UIVut内的UIButton目标动作

[英]UIButton Target Action inside UIView

I have a custom UIView I created in which I have a UIButton . 我有一个我创建的自定义UIView ,其中有一个UIButton Inside that view, I have this code: 在该视图中,我有这样的代码:

func setupViews() {
    menuControlButton.addTarget(self, action: "toggleButton:", forControlEvents: .TouchUpInside)
}

func toggleButton(sender: MenuControlButton!) {
    let isActive = sender.toggleActive() // switches button image and returns whether active or not after
    NSUserDefaults.standardUserDefaults().setBool(isActive, forKey: sender.title)
    someOtherViewController.reloadCalendar()
}

Is this a bad practice to do though? 这是不好的做法吗? Especially since I'm calling another controller's function through a custom UIView . 特别是因为我通过自定义UIView调用另一个控制器的功能。

Should I instead make it so these methods are in the ViewController? 我应该这样做,所以这些方法在ViewController中? Or is there a better way to do this? 或者有更好的方法吗?

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. 作为一种简单的方法,您可以为自定义UIView定义函数addTarget,它将像桥一样工作。

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 然后你的控制器只知道UICustomView可以处理事件而UICustomView对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 如果您在自定义视图中有多个按钮,那么使用UIAlertControllerStyle.ActionSheet类型实现类似UIAlertController的操作可能是个好主意。

Thomas: Please correct me if I understood wrongly. 托马斯:如果我理解错误,请纠正我。 I assume you have one ViewController (SomeOtherViewController) and Custom class for UIView (CustomView). 我假设您有一个ViewController(SomeOtherViewController)和UIView(CustomView)的Custom类。 Inside custom view, you have a button. 在自定义视图中,您有一个按钮。 You are passing the someotherviewcontroller pointer to customview class and reloading the controller view (Calendar). 您正在将someotherviewcontroller指针传递给customview类并重新加载控制器视图(日历)。

If the above scenario is right, then I feel better you create a protocol in CustomView class and implement that protocol method in SomeOtherViewController and reload the data in that. 如果上面的场景是正确的,那么我觉得你在CustomView类中创建一个协议并在SomeOtherViewController中实现该协议方法并重新加载其中的数据感觉更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM