Ios postNotification访问观察者的不同实例

Ios postNotification访问观察者的不同实例,ios,objective-c,cocoa-touch,nsnotificationcenter,Ios,Objective C,Cocoa Touch,Nsnotificationcenter,我有一个类a(监听器),在他的init方法中观察通知,并实例化和膨胀NSMutableArray 当B类(发送方)向a类观察员发布通知时, 它正确地调用选择器中声明的方法,但在该方法中,我的实例变量NSMutableArray指向0x000000 通知是否可以在类的其他位置运行?我可以解决一个单身汉的问题吗 @implementation ClassA @synthesize myArray; -(id) init { if (self = [super init]){ [[N

我有一个类a(监听器),在他的init方法中观察通知,并实例化和膨胀NSMutableArray

当B类(发送方)向a类观察员发布通知时, 它正确地调用选择器中声明的方法,但在该方法中,我的实例变量NSMutableArray指向0x000000

通知是否可以在类的其他位置运行?我可以解决一个单身汉的问题吗

@implementation ClassA
@synthesize myArray;

-(id) init {
    if (self = [super init]){
    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(methodThatGetsCalled:)
                                                     name:@"dispatchMethods"
                                                   object:nil];

        classB = [[ClassB alloc] init];

    }
    return self;
}

- (void)anotherClassAMethod {
   // first i populate my array
   myArray = [[NSMutableArray alloc] initWithArray:eventsArray];
   // than i call Class B
}
- (void)methodThatGetsCalled:(NSNotification)note {
    // when the notification is posted, this method gets called but...
    myArray; //points to 0x000000 here
}

你在这里说的是

[[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(methodThatGetsCalled:)
                                                     name:@"dispatchMethods"
                                                   object:nil];
任何时候,任何人(不管是谁)都会喊出
“dispatchMethods”
所有
ClassA
的实例

因此,您希望更具选择性,并且只在特定实例发布时侦听

classB = [[ClassB alloc] init];

[[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(methodThatGetsCalled:)
                                                     name:@"dispatchMethods"
                                                   object:classB];
在这种情况下,只有当
classB
是发布对象时,您才会听到通知,如从
classB

[[NSNotificationCenter defaultCenter] postNotificationName:@"dispatchMethods" object:self userInfo:nilOrWhatever]

因此,如果您有多个
ClassA
ClassB
的实例,那么另一个实例在
另一个ClassMethod

中实例化数组之前点击了
已缩放的方法:
,您可以发布init方法吗?也许您传递的变量是错误的。让我们看一些代码!完成后,可能是最干净的,最好的方法是在
myArray
初始化中添加断点。您何时调用
另一个ClassMethod
?在init中,我让观测者“打开”到其他类没有问题,我的问题是,当postNotification之后的方法被调用时,它没有访问NSarray中的日期的权限,我确信它在那里。实际上,向所有实例声明变量Global似乎是可行的,但这似乎不是正确的方法,所以回到我的第一点,通知是以某种静态方式调用类还是生成另一个实例?