开发者

NSNotificationCenter and passing data between view controllers and my class/ Am I on the wrong way?

For test purposes I've created an application with UITabBar开发者_Python百科, 3 view controllers and my class DataAnalyzer (till now it analyses nothing:)).

TestAppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   //..... 
    NSArray *allControllers = [self.tabBarController viewControllers];

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    for (UIVideoEditorController * viewController in allControllers) {
        [center addObserver:viewController selector:@selector(useUpdatedData:) name:@"dataUpdated" object:nil];
        NSLog(@"%@", [viewController description]);
    }    

    DataAnalizer *dataAnalizer = [[DataAnalizer alloc] init];
    [center addObserver:dataAnalizer selector:@selector(useUpdatedData:) name:@"dataUpdated" object:nil];
    // dataAnalizer can't be released here? Where should it be done?
    return YES;
}

in 2-nd, 3-rd view controllers and DataAnalyzer class I added the same method

- (void) useUpdatedData:(NSNotification *)note {
    NSLog(@"Notificatio received in *** view controller");

    //do something with [note object] like show it on a label or store it to instance variable
} 

in 1-st view controller I added text field to send a string as notification

- (void)textFieldDidEndEditing:(UITextField *)textField {

    NSLog(@"First viewController textFieldDidEndEditing: value is %@ ", textField.text);
    NSNotificationCenter *note = [NSNotificationCenter defaultCenter];
    [note postNotificationName:@"dataUpdated" object:textField.text];


}

When I send a string, I see NSLog message in console that notification is received, but can do nothing with the string like showing it on a label. I understand that it's because a view controllers load first time and they were not initiated. But why I'm getting NSLog message in console? Can I send a string to dataAnalizer class, do something there and then get a result to second and third view controllers? Thank you in advance for your answers, because it seems all above is a wrong approach.


Apple documentation pretty clearly states that NSNotificationCenter does not retain it's observers, thats why you can't release the dataAnalyzer there - it would be dealloc'd and the notification would attempt to post to a nil reference.

I don't think it's a good idea to loop through an array of controllers and subscribe each one to a notification. The controller isn't guaranteed to be created at that point, and because it doesn't know it's been volunteered to answer a notification it also doesn't know to unsubscribe. Instead, subscribe to the notification(s) in the -init method of each view controller. That ensures the controller has been created and initialized, and makes each controller responsible for it's own actions.

I'm not entirely sure what your question is, could you rephrase it if the above didn't resolve your problem.

Also, be aware NSNotificationCenter will post to all observers, but it is not async - it waits for each one to finish processing the notification before sending to the next object.


You don't use correctly postNotification method. Your application data should be send using userinfo parameter with the method postNotificationName:object:userInfo:

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜