这是一个奇怪的累积错误,它以某种方式多次将我的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验证(@“发布sessionArrived的通知“); - 但是选择器toPagebookWorkspace被调用两次。如果我重复该错误,选择器将被调用3次,依此类推。所以每次我重新编写bug时,越来越多的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
如果您对此有什么了解,请提前感谢您的帮助。
答案 0 :(得分:0)
这是NSNotificationCenter
的预期行为:任何对象都可以添加多个观察者,即使对于相同的名称,对象和选择器也是如此。如果您不希望多次调用您的选择器(并且您似乎不这样做),请通过调用addObserver:
执行反向操作:
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"sessionArrived" object:client];
(重要提示:执行不调用[[NSNotificationCenter defaultCenter] removeObserver:self]
,因为这会导致您的对象失去对其超类可能已注册的所有通知的观察。请参阅{{3}有关为什么这是一个错误的更多信息。)