从未在NSNotificationCenter中调用的selector方法

时间:2012-02-06 17:34:52

标签: iphone ios xcode xcode4.2

我正在尝试使用NSNotificationCenter,由于某种原因,永远不会调用选择器方法。

- (NewsItem *) loadNewsItemDetail:(NewsItem *)currentNewsItem
{
    self.newsItem = currentNewsItem;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DownloadNewsItem) name:@"connectionDidFinishLoadingComplete" object:nil];

    return self.newsItem;
}

- (void) DownloadNewsItem:(NSNotification *) notification
{
    NSString *urlString = [Configuration newsStreamAPIURL:plNewsAPIKey];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];

    (void)[[NSURLConnection alloc] initWithRequest:request delegate:self];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{       
    ...

    [[NSNotificationCenter defaultCenter] postNotificationName:@"connectionDidFinishLoadingComplete" object:nil];

}

根据我提供的内容永远不会调用我的DownloadNewsItem的原因是什么?

谢谢!

2 个答案:

答案 0 :(得分:4)

你的选择器方法需要冒号,因为它需要一个参数(在这种情况下为NSNotification)。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DownloadNewsItem:) name:@"connectionDidFinishLoadingComplete" object:nil];

答案 1 :(得分:1)

您在:

之后忘记了DownloadNewsItem符号
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(DownloadNewsItem:)
                                             name:@"connectionDidFinishLoadingComplete"
                                           object:nil];