简体   繁体   English

我可以让UIControl获得“一次性”事件目标的所有权吗?

[英]Can I make a UIControl take ownership of “disposable” event targets?

In my view controller I'm programmatically creating N buttons in my UIView . 在我的视图控制器中,我是以编程方式在我的UIView创建N个按钮。 Each button needs to do something different, so I've made a custom handler class, which I create N instances of, each initialized with custom data. 每个按钮需要做一些不同的事情,所以我创建了一个自定义处理程序类,我创建了N个实例,每个实例都使用自定义数据进行初始化。 I create them in the body of a for loop and add them as targets, after which I don't need them anymore. 我在for循环的主体中创建它们并将它们添加为目标,之后我不再需要它们了。

However, because UIControl s don't take ownership of their event targets, I need to hang on to these custom handlers myself (in the controller), introducing an extra ivar and the necessary release code. 但是,由于UIControl不接受其事件目标的所有权,因此我需要自己(在控制器中)继续使用这些自定义处理程序,引入额外的ivar和必要的发布代码。 Is there a more elegant way to handle this problem? 是否有更优雅的方式来处理这个问题?

Add those custom handlers to an NSArray that you retain in your controller (whatever controller is creating the buttons). 将这些自定义处理程序添加到您保留在控制器中的NSArray(无论控制器是否在创建按钮)。 Adding an object to an array retains it, so you will know that it is always around. 将对象添加到数组会保留它,因此您将知道它始终存在。

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

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