【问题标题】:ASIHTTPRequest Issue With NSNotificationCenterNSNotificationCenter 的 ASIHTTPRequest 问题
【发布时间】:2012-11-13 19:01:25
【问题描述】:

我使用ASIHTTPRequest 下载图像。我写了这段代码:

+(void)GetImagesURL
{
    NSLog(@"%i",[TripsArray count]);
    for (int i=0;i< [TripsArray count]; i++) 
    {

        NSURL *DetailTripURL = [NSURL URLWithString:[[TripsArray objectAtIndex:i] TripURL]];
        __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:DetailTripURL];
        [request setCompletionBlock:^{
            NSData *TripHTMLData = [request responseData];
            NSLog(@"url :  %@",DetailTripURL);
            [[TripsArray objectAtIndex:i] setTripData:TripHTMLData];
            //NSLog(@"%@",DetailTripHTMLData);
            // 2
            TFHpple *DetailTripParser = [TFHpple hppleWithHTMLData:TripHTMLData];

            // Get Image
            NSString *ImageTripXpathQueryString = @"//div[@class='image_container']/img";
            NSArray *ImageTripNodes = [DetailTripParser searchWithXPathQuery:ImageTripXpathQueryString];
            NSLog(@"%i",[ImageTripNodes count]);
            for (int j=0 ;j<[ImageTripNodes count]; j++)
            {
                [[TripsArray objectAtIndex:i] setImageUrl:[[ImageTripNodes objectAtIndex:j] objectForKey:@"src"]];
                NSLog(@"%@",[[ImageTripNodes objectAtIndex:j] objectForKey:@"src"]);
            }


        }];
        [request setFailedBlock:^{
            NSError *error = [request error];
            NSLog(@"Get Image URL Failed %@", error.localizedDescription);
        }];

        [request startAsynchronous];        
    }
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Get Images URL Success" object:self];
}

我想postNotificationName 调用另一个函数。

问题是NSNotificationCenter,而不是postNotificationName。 有什么帮助吗?

【问题讨论】:

  • 请写下您认为错误在哪里、您的代码应该做什么以及任何其他相关信息
  • 我有带有图像 URL 的 Trips 数组我想通过使用 TFHpple 以异步方式解析 HTML 页面来获取图像 URL,我想如果我得到所有图像下载 PostNotificationCenter
  • 是否有一个对象已将自己添加为名为@9​​87654326@ 的通知的 NSNotificationCenter 观察者?
  • 没有对象添加它自己克雷格

标签: iphone objective-c ios asihttprequest nsnotificationcenter


【解决方案1】:

首先,你需要添加一个观察者Get_Images_URL_Success

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotMessages:) name:Get_Images_URL_Success object:nil];

当你在+GetImagesURL方法中postNotificationName时,这个会被调用

- (void)gotMessages:(id)sender {
    NSLog(@"This will be called after the postNotificationName");
}

建议您在不需要时removeObserver(例如dealloc

[[NSNotificationCenter defaultCenter] removeObserver:self name:Get_Images_URL_Success object:nil];

现在当你像这样postNotificationName 时,应该调用gotMessages 方法

[[NSNotificationCenter defaultCenter] postNotificationName:Get_Images_URL_Success object:self];

[超级简单,不是吗?]

【讨论】:

  • 这是我做什么 Warif 但不调用 GotMessage ?
  • 您正在发布通知 Get_Images_URL_Success。每次您使用 Get_Images_URL_Success 发布通知时,都会触发 gotMessages。
  • 我可以用参数 Int 调用 GotMessages 吗?
  • 是的。但是您需要使用 NSNumber 而不是整数。可以直接使用字符串。
  • 请你写下我如何调用参数类型为 NSNumber 的方法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多