0

这是一个奇怪的累积错误,它以某种方式将我的 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(@"Posting notification for sessionArrived"); – 但是选择器 toPagebookWorkspace 被调用了两次。如果我重复这个错误,选择器会被调用 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

如果您有任何想法,非常感谢您提前提供的帮助。

4

1 回答 1

0

这是预期的行为NSNotificationCenter:任何对象都可以添加多个观察者,即使对于相同的名称、对象和选择器也是如此。如果你不想让你的选择器被多次调用(而且你似乎没有这样做),addObserver:通过调用来做相反的事情:

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

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

于 2013-10-09T00:05:59.923 回答