0

我使用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。请问有什么帮助吗?

4

1 回答 1

0

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

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

当你postNotificationName+GetImagesURL方法中,这将被调用

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

[超级简单,不是吗?]

于 2012-11-13T19:33:52.577 回答