开发者

Can I make a UIControl take ownership of "disposable" event targets?

开发者 https://www.devze.com 2023-02-16 11:31 出处:网络
In my view controller I\'m programmatically creating N buttons in my UIView. Each button needs to do something different, so I\'ve made a custom handler class, which I create N instances of, each init

In my view controller I'm programmatically creating N buttons in my UIView. 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. I create开发者_开发技巧 them in the body of a for loop and add them as targets, after which I don't need them anymore.

However, because UIControls 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. 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). Adding an object to an array retains it, so you will know that it is always around.


Associative references?

0

精彩评论

暂无评论...
验证码 换一张
取 消