【发布时间】:2011-02-21 00:11:33
【问题描述】:
我想从我的其他一个名为“Properties”的类中重新加载另一个名为“WriteIt_MobileAppDelegate”的类中的表视图。我尝试通过 NSNotificationCenter 类来执行此操作 - 日志被调用但表从未更新。
Properties.h:
[[NSNotificationCenter defaultCenter] postNotificationName:@"NameChanged"
object:[WriteIt_MobileAppDelegate class]
userInfo:nil];
WriteIt_MobileAppDelegate.m
-(void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(reloadItProperties:) name:@"NameChanged" object:self];
}
- (void) reloadItProperties: (NSNotification *)notification {
NSLog(@"Reloading Data"); //this gets called
[[self navigationController] dismissModalViewControllerAnimated:YES];
[self.navigationController popToRootViewControllerAnimated:YES];
[self.tblSimpleTable reloadData];
[self.tblSimpleTable reloadSectionIndexTitles];
// but the rest doesn't
}
我在这里做错了什么?
【问题讨论】:
-
您是否确认底层数据在到达 reloadItProperties 之前已更新?在该 NSLog 行中添加更新数据的日志记录。此外,在 postNotificationName 中,您可能需要
self而不是[WriteIt_MobileAppDelegate class]。 -
是的,数据已更改。添加“self”什么也没做,它只是阻止了 NSLog 出现。我可以以不同的方式调用选择器吗?
-
虽然不是问题,但在 postNotificationName 中,您可能需要 self.但是,正如 ChriB 提到的,在
addObserver中,您应该将对象参数从 self 更改为 nil。有了 self ,您将只收听来自 self 的通知(即 WriteIt)。虽然它没有解释为什么 reloadItProperties 无论如何都会被调用。除了 NameChanged 的 postNotificationName 之外,还有其他代码调用 reloadItProperties 吗? -
它现在可以工作了。DyingCactus,将其更改为 nil 改变了一切。非常感谢!
-
发表你的评论作为答案,我会给你绿色的勾:)
标签: iphone class uitableview selector nsnotificationcenter