我正在 appDelegate 中创建NSNotification
。
- (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
}
我在其他课程上发布通知:
-(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];
}
问题是从不调用checkRegister。
答案 0 :(得分:2)
@"checkRegister"
通知的目的是什么?在发布通知之前,您if(strUserName != NULL)
检查了if,但我没看到您在strUserName
设置了NSLog(@"%@", strUserName)
一定要试试strUserName
。我很确定它是空的。如果NSXMLParser
来自您解析的XML数据,则应在XML解析代码之后发布通知,该代码将在您实现的[xmlParser parse]
委托方法中。这将是one of these you use.
if(strUserName != NULL)
[[NSNotificationCenter defaultCenter] postNotificationName:@"checkRegister" object:nil];
没有阻止,所以你的
xmlParser
在postNoticationName:
完成解析之前将调用,因此,你的strUserName尚未设置,这就是它为零的原因而你的[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkRegister:) name:@"checkRegister" object:nil];
将不会被调用
编辑:
把你的
application:didFinishLaunchingWithOptions:
在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkRegister:) name:@"checkRegister" object:nil];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.mainViewController = [[[MainViewController alloc] initWithNibName:@"MainViewController_iPhone" bundle:nil] autorelease];
} else {
self.mainViewController = [[[MainViewController alloc] initWithNibName:@"MainViewController_iPad" bundle:nil] autorelease];
}
self.window.rootViewController = self.mainViewController;
[self.window makeKeyAndVisible];
return YES;
}
的最顶部,您很可能在注册通知之前在主视图中发布通知,例如
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.mainViewController = [[[MainViewController alloc] initWithNibName:@"MainViewController_iPhone" bundle:nil] autorelease];
} else {
self.mainViewController = [[[MainViewController alloc] initWithNibName:@"MainViewController_iPad" bundle:nil] autorelease];
}
self.window.rootViewController = self.mainViewController;
[self.window makeKeyAndVisible];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkRegister:) name:@"checkRegister" object:nil];
return YES;
}
将起作用,而
MainControllerView
如果您使用viewDidLoad
{{1}}方法完成数据加载,将无效。