iPhone SDK - 通知多次触发,并将多个视图推送到堆栈上

发布于 2024-10-07 05:29:08 字数 1731 浏览 4 评论 0原文

好吧,这实际上已经解决了 - 但我不明白为什么我所做的事情有效。

我的问题是,发送一次通知会导致一个事件多次触发。我最终在堆栈上得到了几个不需要的视图。简而言之:

用户按下工具栏中的按钮,委托会发送一条通知,

mapItem = [[UIBarButtonItem alloc] initWithImage:mapImage style: UIBarButtonItemStylePlain target:self action:@selector(mapButtonPressed:)];

-(void)mapButtonPressed:(id)sender{
    NSLog(@"Map Button Pressed");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"mapButtonPressed" object:nil ] ;
}

这会触发当前视图中的一个函数,将地图视图推送到堆栈上。

-(void)openListMap:(NSNotification *)aNotification {

    mapViewController = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];

    NSLog(@"Map Created");

    mapViewController.searchLocation = searchLocation;
    if(givenLocationType == @"input"){
        mapViewController.inputLocationText = inputLocationText;
    }
    mapViewController.givenLocationType = givenLocationType;

    CultureNOWAppDelegate *delegate =
    [[UIApplication sharedApplication] delegate];
    [delegate.navigationController pushViewController:mapViewController
                                      animated:YES];

}

现在可以了,我将最后一行从: 更改

CultureNOWAppDelegate *delegate =
    [[UIApplication sharedApplication] delegate];
    [delegate.navigationController pushViewController:mapViewController
                                      animated:YES];

为:

[self.navigationController pushViewController:mapViewController animated:YES];

结果是,尽管 openListMap 函数仍然触发多次(您可以在控制台中看到,日志输出显示“Map Created” for自应用程序启动以来每次视图出现时)它只将最新的mapView推送到堆栈上。

但为什么?为什么它一开始会多次触发,为什么它会通过交换两段代码而停止,而这两段代码在所有意图和目的上都是相同的?

感谢您的任何想法。

Ok, so this is actually solved - but I don't understand why what I did worked.

My issue was that sending a notification once would cause one event to fire multiple times. I ended up with several unwanted views on the stack. in a nut shell:

the user presses a button in the toolbar, a notification is sent from the delegate

mapItem = [[UIBarButtonItem alloc] initWithImage:mapImage style: UIBarButtonItemStylePlain target:self action:@selector(mapButtonPressed:)];

-(void)mapButtonPressed:(id)sender{
    NSLog(@"Map Button Pressed");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"mapButtonPressed" object:nil ] ;
}

this fires a function in the current view to push a map view onto the stack.

-(void)openListMap:(NSNotification *)aNotification {

    mapViewController = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];

    NSLog(@"Map Created");

    mapViewController.searchLocation = searchLocation;
    if(givenLocationType == @"input"){
        mapViewController.inputLocationText = inputLocationText;
    }
    mapViewController.givenLocationType = givenLocationType;

    CultureNOWAppDelegate *delegate =
    [[UIApplication sharedApplication] delegate];
    [delegate.navigationController pushViewController:mapViewController
                                      animated:YES];

}

This works now, I changed the last line from:

CultureNOWAppDelegate *delegate =
    [[UIApplication sharedApplication] delegate];
    [delegate.navigationController pushViewController:mapViewController
                                      animated:YES];

to:

[self.navigationController pushViewController:mapViewController animated:YES];

The result is that, although the openListMap function still fires multiple times (you can see in the console, the log output shows "Map Created" for each time the view has appeared since the app started) it only pushed the latest mapView onto the stack.

But why? Why was it firing multiple times in the first place, and why has it stopped by exchanging two piece of code which, for all intents and purposes, are the same?

Thanks for any thoughts.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带上头具痛哭 2024-10-14 05:29:08

它被触发了 2 次的事实警告我,你的“修复”并不是真正的修复 - 这是 Apple 的 API 的一种行为,它碰巧做了你想要的事情。

我在一个应用程序中遇到了类似的问题,其中通知莫名其妙地被触发了两次。我后来意识到,通知只被调用一次 - 但在 NSNotificationCenter 内,没有什么可以阻止您为完全相同的选择器回调注册完全相同的事件的两个观察者。

这种情况发生在我们身上,因为我们在 viewDidLoad 中添加了观察者,但从未在 viewDidUnload 中删除了观察者。然后,当用户的手机内存不足时(感谢 Apple,这种情况在 iOS4+ 中经常发生),视图会被刷新,当第二次调用 viewDidLoad 时,您最终会得到 2 个观察者。

这可能不是您的确切问题 - 但我想听听您在哪里注册观察员通知。

The fact that it is firing 2 times warns me that your "fix" is not really a fix - it is a behavior of Apple's API that happens to do what you want.

I had a similar issue with an app where a notification was inexplicably being fired twice. What I realized later was that the notification is only called once - but that inside NSNotificationCenter, there is nothing to stop you from registering TWO observers for the exact same event for the exact same selector callback.

This happened to us because we added observers in viewDidLoad, but never removed the observers in viewDidUnload. Then, when the user's phone had low memory (which, thanks Apple, happens a lot in iOS4+), the views get flushed, and you end up with 2 observers when viewDidLoad gets called a second time.

That may not be your exact problem - but I would like to hear about where you are registering for observer notifications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文