gpt4 book ai didi

objective-c - 特定对象的 nsnotification

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:15:27 26 4
gpt4 key购买 nike

我正在使用 xcode 和 objective c 开发 ios 应用程序 ios 5+。好的,目前正在处理 nsnotifications,我只是需要一些说明,因为我有点困惑。假设我有一个 View Controller ,我添加了一个观察者,就像这样

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showContent:) name:kTPSShowContentNotification object:self];

对象设置为self的地方。我认为这意味着如果从该对象发送,它只会从该通知中查找。我错了吗?

在代码的其他地方,我像这样放置一个通知

 [[NSNotificationCenter defaultCenter] postNotificationName:kTPSShowContentNotification object:currentVC];

其中 currentVC 是最初设置观察者的 View Controller 。我认为这就是捕获该通知所需的全部内容,因为帖子告诉通知中心从该 View Controller 发送它。但它没有捕获它,我不确定为什么。如果在添加观察者时我将对象设置为 nil 那么它会捕获它但是所有其他具有该通知观察者的 View Controller (如果有的话)也会捕获它。有没有办法解决 ?我是不是完全错了?

最佳答案

要仅接收来自theObjectSendingNotification 对象的通知,您应该这样写:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showContent:) name:kTPSShowContentNotification object:theObjectSendingNotification];

发送通知的对象应该这样发送

 [[NSNotificationCenter defaultCenter] postNotificationName:kTPSShowContentNotification object:self];

关于objective-c - 特定对象的 nsnotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10815381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com