2012-01-31 64 views
0

我在appDelegate中创建NSNotificationNSNotification不会调用选择器

- (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

'strUserName'在哪里设置?你怎么知道通知正在发布?你有没有在这条线上放置一个断点并确认它被调用? – user1118321 2012-01-31 06:19:44

+0

它在解析中设置。这不是问题。我无法调用checkRegister方法。 – Nitish 2012-01-31 06:21:21

回答

2

什么是你@"checkRegister"通知的目的,你有你的if(strUserName != NULL)检查,如果您发布通知之前,但我没有看到你在哪里设置该strUserName所以肯定,尝试NSLog(@"%@", strUserName) 。我很确定它是空的。如果strUserName来自您解析的XML数据,则应在XML解析代码之后发布通知,该代码将位于您实施的NSXMLParser委托方法中。这将是one of these you use.

[xmlParser parse]不会阻塞,所以你

if(strUserName != NULL) 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"checkRegister" object:nil]; 

将被调用,之前xmlParser完成解析,那么,你的strUserName中尚未设定,这就是它是零和的原因,你的postNoticationName:将不会被调用

编辑:

把你

[[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; 
} 

如果您的数据加载在MainControllerViewviewDidLoad方法中完成将无法正常工作。

+0

X斜线:你不理解我的观点。我添加委托调用,当我创建通知并添加选择器中的checkRegister在同一类。但选择器从未被调用过。你所说的也是正确的,但是后面的部分。首先,应该调用selecotr。 – Nitish 2012-01-31 07:04:19

+0

选择器不会被调用,因为你的strUserName == nil。这是因为你在NSXML委托中设置它,在你的postNotificationName:时尚未完成。如果你不相信我,只需从代码中删除(strUserName!= NULL)即可。 – 2012-01-31 07:20:34

+0

我评论过它。仍然选择器未被调用。 – Nitish 2012-01-31 07:26:36

相关问题