Pages

Wednesday, February 6, 2013

How addObserver works in xCode

Follow the following steps for addObserver

1. From where you want to listen the Observer, add the following lines inside your event function

Supose its Aclass.m


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess) name:@"loginSuccessNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginFailure) name:@"loginFailureNotification" object:nil];


2. Now add the postNotification in the class from where you want to dispatch for this


if(validUser)
        [[NSNotificationCenter defaultCenter] postNotificationName:@"loginSuccessNotification" object:nil];
    else
        [[NSNotificationCenter defaultCenter] postNotificationName:@"loginFailureNotification" object:nil];



3. Now remove this Notification when you have done

- (void) loginSuccess {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)loginFailure {
    [[NSNotificationCenter defaultCenter] removeObserver:self];




No comments:

Post a Comment