I am creating a NSNotification in appDelegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkRegister:) name:@"checkRegister" object:nil];
return YES;
}
-(void)checkRegister:(NSNotification *) notification{
//Some code
}
And I am posting notification in some other class:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",theXML);
[theXML release];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];
[xmlParser release];
[connection release];
[webData release];
if(strUserName != NULL)
[[NSNotificationCenter defaultCenter] postNotificationName:@"checkRegister" object:nil];
}
The problem is that checkRegister is never called.
What is the purpose of your
@"checkRegister"notification, you haveif(strUserName != NULL)check in your if before you post the notification, but I don’t see where do you setup thatstrUserNameso definitely, tryNSLog(@"%@", strUserName). I’m pretty sure it’s null. if thatstrUserNameis from the XML data your parse, you should post the notification after XML parsing code, which will be in yourNSXMLParserdelegate methods you implement. Which will be one of these you use.[xmlParser parse]doesn’t block, so yourwill be called, before
xmlParserfinished parsing, so, your strUserName is not set yet, and that’s the reason it is nil and yourpostNoticationName:will not be calledEDIT:
put your
on the very top of your
application:didFinishLaunchingWithOptions:, it is very likely you post the notification in your main view before you registered for notification, for examplewill work, while
will not work if your data loading is done in
MainControllerViewviewDidLoadmethod.