2012-01-31 65 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

相關問題