一个NSNotion发送,但许多选择器调用


这是一个

奇怪的累积错误,它以某种方式将我的VC多次推到Nav VC上。

我有一个UINavigationController,rootViewController设置为CWLandingVC(lvc(。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    CWLandingVC *lvc = [[CWLandingVC alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:lvc];
...
}

在 lvc 上,用户登录,当我的 APIClient 类获得成功的服务器响应时,它会发布通知:

     NSNotification* notification = [NSNotification notificationWithName:@"sessionArrived" object:self];
     NSLog(@"APIClient Posting notification for sessionArrived");
     [[NSNotificationCenter defaultCenter] postNotification:notification];

lvc 会侦听此内容并相应地发送此选择器:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
           selector:@selector(toPagebookWorkspace)
               name:@"sessionArrived"
             object:client];
...
- (void)toPagebookWorkspace {
NSLog(@"lvc Calling toPagebookWorkspace for session %@.  Opening PagebookWorkspace view.", [self sessionId]);
CWWorkspaceVCViewController *wvc = [[CWWorkspaceVCViewController alloc] init];
[[self navigationController] pushViewController:wvc animated:YES];
}

当用户登录,成功执行pushViewController:wvc,注销回lvc,然后重新登录时,就会出现此错误。 当他们这样做时,通知会再次发布——我用 NSLog 验证(@"发布会话到达通知"(;– 但是页面书工作区的选择器被调用了两次。 如果我重复该错误,则选择器被调用 3 次,依此类推。 因此,每次我重现该错误时,越来越多的wvc在UINavigationController中被推到彼此之上。

也许这些日志可以帮助阐明我所看到的奇怪的累积序列。 对于每个 APIClient 通知帖子,我都会收到越来越多的 pushViewController:wvc,而不仅仅是 1 次推送。

     Logging in...
     APIClient Posting notifcation for sessionArrived
     lvc Calling toPagebookWorkspace for session lvcke2.  Opening PagebookWorkspace view.
     Pressed back on nav bar, calling viewWillDisappear

     Logging in...
     APIClient Posting notifcation for sessionArrived
     lvc Calling toPagebookWorkspace for session lvcke2.  Opening PagebookWorkspace view.
     lvc Calling toPagebookWorkspace for session lvcke2.  Opening PagebookWorkspace view.
     nested push animation can result in corrupted navigation bar
     Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
     Unbalanced calls to begin/end appearance transitions for <CWWorkspaceVCViewController: 0x8067410>.
     Pressed back on nav bar, calling viewWillDisappear
     Pressed back on nav bar, calling viewWillDisappear

     Logging in...
     APIClient Posting notifcation for sessionArrived
     lvc Calling toPagebookWorkspace for session lvcke2.  Opening PagebookWorkspace view.
     lvc Calling toPagebookWorkspace for session lvcke2.  Opening PagebookWorkspace view.
     nested push animation can result in corrupted navigation bar
     lvc Calling toPagebookWorkspace for session lvcke2.  Opening PagebookWorkspace view.
     nested push animation can result in corrupted navigation bar
     Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
     Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
     Unbalanced calls to begin/end appearance transitions for <CWWorkspaceVCViewController: 0x8068330>.
     Pressed back on nav bar, calling viewWillDisappear
     Pressed back on nav bar, calling viewWillDisappear
     Pressed back on nav bar, calling viewWillDisappear

     Logging in...
     APIClient Posting notifcation for sessionArrived
     lvc Calling toPagebookWorkspace for session lvcke2.  Opening PagebookWorkspace view.
     lvc Calling toPagebookWorkspace for session lvcke2.  Opening PagebookWorkspace view.
     nested push animation can result in corrupted navigation bar
     lvc Calling toPagebookWorkspace for session lvcke2.  Opening PagebookWorkspace view.
     nested push animation can result in corrupted navigation bar
     lvc Calling toPagebookWorkspace for session lvcke2.  Opening PagebookWorkspace view.
     nested push animation can result in corrupted navigation bar
     Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
     Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
     Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
     Unbalanced calls to begin/end appearance transitions for <CWWorkspaceVCViewController: 0x7257930>.
     Pressed back on nav bar, calling viewWillDisappear
     Pressed back on nav bar, calling viewWillDisappear
     Pressed back on nav bar, calling viewWillDisappear
     Pressed back on nav bar, calling viewWillDisappear

如果您知道发生了什么,请提前感谢您的帮助。

这是

NSNotificationCenter的预期行为:任何对象都可以添加多个观察者,即使对于相同的名称、对象和选择器也是如此。如果您不想多次调用选择器(而且看起来您不希望这样做(,请通过调用来执行addObserver:的反向操作:

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"sessionArrived" object:client];

(重要说明:不要调用[[NSNotificationCenter defaultCenter] removeObserver:self],因为这会导致对象失去对其超类可能已注册的所有通知的观察。有关为什么这是一个错误的更多信息,请参阅此链接。

最新更新