开发者

iPhone SDK - Notification firing multiple times, and pushing multiple views onto the stack

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 = [[UIB开发者_Python百科arButtonItem 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜