【问题标题】:iPhone - Launching selectors from a different classiPhone - 从不同的类启动选择器
【发布时间】: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


【解决方案1】:

好像你使用了object参数错误:

addObserver:selector:name:object:

通知发送者
其对象 观察者想要的通知 接收;
也就是说,只有通知 此发件人发送的被递送到 观察者。如果你通过 nil,则 通知中心不使用 通知的发件人决定 是否传递给观察者。

【讨论】:

  • 我不这么认为,[WriteIt_MobileAppDelegate 类] 是唯一甚至可以切换 NSLog 的对象。是否有另一种切换选择器以更新表格视图的方法?
猜你喜欢
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
  • 2019-03-02
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-04
相关资源
最近更新 更多