独立重用已声明的强大属性
我创建了一个类(MyClass)并需要它的多个实例,每个实例将包含多个计时器、文本字段和标签。由于 ARC,当计时器失效时,目标就会被释放,但有时我会让它们失效并重新启动它们,所以我不能让它们释放。因此,我转到 AppDelegate(这是创建 MyClass 实例的类)并将 MyClass 声明为强属性。 @property (strong) MyClass *myInstance;
这部分解决了问题,每当我创建另一个实例时,前一个实例就会丢失它的引用,并且如果我尝试在旧实例中重新启动 NSTimer,我访问错误。如果我重新启动最后计时器,那就没有问题了。
我相信,由于 myInstance 是一种属性,每当我创建一个新属性时,AppDelegate 都会重写旧的属性,从而丢失旧的引用。我需要能够保留强属性,但以某种方式使其对每个实例独立工作,或者找到另一种方法使 myInstance 成为强引用,而不必是属性。
I created a class (MyClass) and need several instances of it, each will hold several timers, textfields and labels. Because of ARC, the target was getting deallocated when the timers were invalidated, but I sometimes invalidate them to restart them so I can't let them deallocate. So I went to my AppDelegate (which is the class that creates the instances of MyClass) and declared MyClass as a strong property. @property (strong) MyClass *myInstance;
This partially works the problem is that whenever I create another instance, the previous instance loses it's reference, and if I try to restart the NSTimer in an old instance, I get BAD ACCESS. If I restart the last timer there's no problem.
I believe that since myInstance is a property, whenever I make a new one the AppDelegate rewrites the old one, losing the old references. I need to either be able to keep the strong property but somehow make it work independently for each instance, or find another way to make myInstance a strong reference, without it having to be a property.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的实例放入 NSSet 或 NSArray 等容器中。
Put your instances in a Container like NSSet or NSArray.