Search code examples
iphoneobjective-cnsnotificationcenter

iphone - notification not being fired


Goal:

I want to use the Observer Pattern so that when one uiimageview receives a different background image, then 2 other uiimageviews will listen for that change, and then change themselves.

Strategy:

Based on what I read about observer pattern in objective-c, I decided to implement the nsnotificationcenter.

Code:

self refers to the RemoteViewManagerController, updateButtons is the method that will be called when the ImageSwap event is fired, and object refers to the "main" uiimageview, that is, the uiimageview that when changed will cause changes in other uiimageviews.

- (void)registerButtonObserver:(UIView *)currentView
{  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateButtons:) name:@"ImageSwap" object:[self.view viewWithTag:1]];
}

setDefaultButtons is invoked, and we iterate through the buttons and target buttons based on tag. The "main" uiviewimage has a tag of 1. So we call setImageChange to change the background image of that button, and as a result, I want to fire the ImageSwap event, to change the other two uiimageview buttons, and I pass in those buttons part of the userinfo dictionary. The idea is when updateButtons is invoked, I can reference those buttons in the userinfo dictionary.

- (void)setDefaultButtons:(UIView *)currentView
{
    for( UIView *view in currentView.subviews ) {  
        if( [view isKindOfClass:[UIButton class]] ) {  
            if( view.tag == 1 ){                
                [self setImageChange:@"fence" forButton:view];

                NSArray *keys = [NSArray arrayWithObjects:@"subview1", @"subview2", nil];
                NSArray *objects = [NSArray arrayWithObjects:[self.view viewWithTag:4], [self.view viewWithTag:5], nil];
                NSDictionary *items = [NSDictionary dictionaryWithObjects:objects 
                                                                       forKeys:keys];
                NSLog(@"But we sure to get here right");
                [[NSNotificationCenter defaultCenter]postNotificationName:@"ImageSwap" object:view userInfo:items];                
            }
            else if(view.tag == 2){
                [self setImageChange:@"siren" forButton:view];
            }
            else if(view.tag == 3){
                [self setImageChange:@"auxiliary" forButton:view];
            }
        }  
    }
}

Note that I know that we get to the postNotificationName line, because this line does fire: NSLog(@"But we sure to get here right");

I don't get any errors. But this line in RemoteViewManagerController.m:

- (void)updateButtons:(NSNotification*)notification 
{
    NSLog(@"Do we get here?");
}

is never called.


Solution

  • I believe that when two subviews have the same tag, -viewWithTag: just returns the first one that it finds. So if there happen to be two views with tag=1, it's quite possible that you're observing the wrong one. Try changing the object parameter in you -addObserver... call to nil, which will indicate that you want to observe that notification for all objects.