简体   繁体   English

呼叫通知时为BAD_ACCESS

[英]BAD_ACCESS when calling Notification

I have class A and class BI am calling class B from class A.Here my problem is width and height of class A is depending on class B.when sizeForScrollView property (class B property) changed i want notification.everything is working fine.But when i am reloading class A at that time it is crashing from class B notification line. 我有A类和BI类正在从A类调用B类。这是我的问题是A类的宽度和高度取决于B类。当sizeForScrollView属性(B类属性)更改时,我想要通知。 sizeForScrollView正常。当我当时重新加载A类时,它从B类通知行崩溃。

Here is code : 这是代码:

class A A级

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (changeContentSize) name:@"MyNotification" object:nil];
-(void)changeContentSize{
    self.scrollView.contentSize = CGSizeMake(self.aSubjectView.sizeForScrollView.width, self.aSubjectView.sizeForScrollView.height);
    self.aSubjectView.frame = CGRectMake(frameForView.origin.x, frameForView.origin.y, frameForView.size.width, self.aSubjectView.sizeForScrollView.height);

}

class B B级

CGRect rect;
rect.size.width = self.frame.size.width;
rect.size.height = heightForSubject + 10;
rect.origin = self.frame.origin;
sizeForScrollView = rect.size;
NSNotification* notification = [NSNotification notificationWithName:@"MyNotification" object:self];
        [[NSNotificationCenter defaultCenter] postNotification:notification];

Please help me.Thanking you. 请帮助我。谢谢。

Make sure instances of class A are removing themselves as observers on dealloc. 确保类A的实例在dealloc上将自己作为观察者删除。 Otherwise if you release an instance, the notification center will still try to talk to it after it's been released, causing an EXC_BAD_ACCESS crash. 否则,如果您释放实例,则通知中心在释放后仍会尝试与之交谈,从而导致EXC_BAD_ACCESS崩溃。

If you're not using ARC, that would look something like this (in class A): 如果您不使用ARC,则看起来像这样(在A类中):

- (void)dealloc 
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc]; // Take this line out if you are using ARC
}

This is necessary because adding an object as an observer does not increment its retain count. 这是必需的,因为将对象添加为观察者不会增加其保留计数。 The notification center doesn't take ownership of the observer or do anything to track whether it's still around or not. 通知中心不会获得观察者的所有权,也不会采取任何措施来跟踪观察者是否还在附近。

在viewDidUnload中删除“ MyNotification”的观察者

 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyNotification" object:nil];

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

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