ios - 未从模态视图 Controller 收到 NSNotification
<p><p>我在这里缺少什么?我只是想从模态视图 Controller 向启动它的 ViewController 发送一个简单的通知,但什么也没有收到。</p>
<p>这是 ViewController 中启动模态转场的代码:</p>
<pre><code>- (IBAction) chooseSuperGroup:(UIButton *)sender {
NSLog(@"super group choice about to be made");
[ addObserver:self
selector:@selector(choiceReceived:)
name:@"selectionMade"
object:self];
}
- (void) choiceReceived: (NSNotification *) notification
{
NSLog(@"here");
if ([ isEqualToString:@"selectionMade"]) {
NSLog(@"received");
NSLog(@"%@", (NSString *));
}
[ removeObserver:self
name: @"selectionMade"
object:self];
}
</code></pre>
<p>然后,在模态视图 Controller 中,当用户从表格 View 中选择一个单元格时,将执行此代码:</p>
<pre><code>NSDictionary *dict = ;
NSLog(@"printing dictionary contents");
for (id key in dict) {
NSLog(@"key: %@ object: %@", key, );
}
[ postNotificationName:@"selectionMade" object:self userInfo:dict];
</code></pre>
<p>我的输出如下所示:</p>
<pre><code>Super group choice about to be made
printing dictionary contents
key: superGroup object: myChoice
</code></pre>
<p>所以选择被捕获并添加到字典中。但没有证据表明收到任何通知。这不可能那么难,但我没有看到我的错误。有人能帮我吗?谢谢!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>尝试使用“nil”而不是“self”</p>
<p>//添加观察者 </p>
<pre><code> [ addObserver:self selector:@selector(choiceReceived:) name:@"selectionMade" object:nil];
</code></pre>
<p>//移除观察者</p>
<pre><code>[ removeObserver:self
name: @"selectionMade"
object:nil];
</code></pre>
<p>//发布通知</p>
<pre><code>[ postNotificationName:@"selectionMade" object:nil userInfo:dict];
</code></pre>
<p>引用:<a href="https://stackoverflow.com/a/8188759/2449268" rel="noreferrer noopener nofollow">https://stackoverflow.com/a/8188759/2449268</a> </p></p>
<p style="font-size: 20px;">关于ios - 未从模态视图 Controller 收到 NSNotification,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/20986243/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/20986243/
</a>
</p>
页:
[1]