委托似乎自动设置为 nil
我正在制作一个助手类。这个帮助器类由一个我将与之交互的 NSObject 类和一个执行其他一些操作的 UIViewController 组成。
这是在我的应用程序委托中或在我分配 NSObject 类的任何地方发生的情况:
MyObjectClass *class = [[MyObjectClass alloc] init];
[class setDelegate:self];
现在,这个委托是我这样创建的:
@protocol MyDelegate
-(void) someCall;
@end
@interface MyHelperClass : NSObject {
id <MyDelegate> delegate;
}
@property (retain, nonatomic) id <MyDelegate> delegate;
所以这很好用。但问题就在这里。如果我分配我的帮助器类并将其委托设置为视图控制器或其他东西,然后调用打开我的帮助器视图控制器的方法之一,委托将停止工作。调试后,它似乎有一些内存地址,直到我弹出另一个视图控制器,当我返回它时,委托的内存地址是 0x0。
我的问题是怎么会发生这种情况?我需要对我的代表做一些特别的事情来阻止它发布吗? (我自己不会在任何地方发布它)我需要保留它吗? 即使打开视图控制器,如何保持分配状态以便我可以继续使用它?
提前致谢!
I am making a helper class. This helper class consists of a NSObject class which I would interact with and a UIViewController that does some other stuff.
Here is what happens, in my app delegate or wherever I allocate my NSObject class:
MyObjectClass *class = [[MyObjectClass alloc] init];
[class setDelegate:self];
Now, this delegate is something I created like this:
@protocol MyDelegate
-(void) someCall;
@end
@interface MyHelperClass : NSObject {
id <MyDelegate> delegate;
}
@property (retain, nonatomic) id <MyDelegate> delegate;
So this works great. But here's the problem. If I allocate my helper class and set it's delegate to a view controller or something and then call one of my methods that open my helper view controller the delegate stops working. After debugging it seems it has some memory address until I pop up the other view controller and when I return to it, the memory address of the delegate is 0x0.
My questions is how could this happen? Do I need to do something special with my delegate to stop it from releasing? (I don't release it anywhere myself) Do I need to retain it? How do I keep it allocated even when I open my viewcontroller so I can continue using it?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
委托的内存地址是0x0意味着它从未被分配,而不是被释放。如果它被释放,它就不再是一个有效的地址,但它将与它被释放之前的地址相同。
the memory address of the delegate is 0x0 means that it was never assigned, not that it was released. if it was released it wouldn't be a valid address anymore, but it would be the same address that it was before it was released.