iosviewuikitnstimer

How to reload a view every minute in iOS?


I have the following problem - I have a radio app. I am parsing the current song's title from a web HTML element and displaying it in a label. I am loading this element every minute in my function with an NSTimer to see if it has been changed. Here is my function:

-(void)parsing
{
    NSURL *url = [NSURL URLWithString:@"http://live.radioeuphoria.bg:41232/played.html?sid=1"];
    NSData *webData = [NSData dataWithContentsOfURL:url];
    NSString *xPathQuery = @"//tr[2]/td[2]";
    TFHpple *parser = [TFHpple hppleWithHTMLData: webData];
    NSArray *array = [parser searchWithXPathQuery:xPathQuery];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        CLTickerView *ticker = [[CLTickerView alloc] initWithFrame:CGRectMake(200, 798, 556, 50)]; // movable text
        for (TFHppleElement *element in array)
        {
            ticker.marqueeStr = [[element firstChild] content];
        }
    // ticker.marqueeStr = @"Current song's title";
        ticker.marqueeFont = [UIFont boldSystemFontOfSize:42];
        [self.view addSubview:ticker];
        NSLog(@"bla bla");
    }
    else
    {
        CLTickerView *ticker = [[CLTickerView alloc] initWithFrame:CGRectMake(86, 346, 230, 30)]; // movable text
        for (TFHppleElement *element in array)
        {
            ticker.marqueeStr = [[element firstChild] content];
        }
        ticker.marqueeFont = [UIFont boldSystemFontOfSize:22];
        [self.view addSubview:ticker];
        NSLog(@"bla bla");
    }
}

In my viewDidLoad I am doing this:

- (void)viewDidLoad
{
    [self parsing];
    timer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(parsing) userInfo:nil repeats:YES];
    [super viewDidLoad];

}

In that way every single minute the label appears on the old one, instead of replacing it. How to replace the old view which I am displaying ([self.view addSubview:ticker];) with the new one?


Solution

  • once refer this one

         -(void)parsing
            {
                NSURL *url = [NSURL URLWithString:@"http://live.radioeuphoria.bg:41232/played.html?sid=1"];
                NSData *webData = [NSData dataWithContentsOfURL:url];
                NSString *xPathQuery = @"//tr[2]/td[2]";
                TFHpple *parser = [TFHpple hppleWithHTMLData: webData];
                NSArray *array = [parser searchWithXPathQuery:xPathQuery];
                if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
                {
                    CLTickerView *ticker = [[CLTickerView alloc] initWithFrame:CGRectMake(200, 798, 556, 50)]; 
        // here every time you are creating ticker that's why it overlaps remove this on here and add in viewDidload (define ticker globally). 
    
                    for (TFHppleElement *element in array)
                    {
                        ticker.marqueeStr = [[element firstChild] content];
                    }
                // ticker.marqueeStr = @"Current song's title";
                    ticker.marqueeFont = [UIFont boldSystemFontOfSize:42];
                    [self.view addSubview:ticker];
                    NSLog(@"bla bla");
                }
                else
                {
                    CLTickerView *ticker = [[CLTickerView alloc] initWithFrame:CGRectMake(86, 346, 230, 30)];
     // here every time you are creating ticker that's why it overlaps remove this on here and add in viewDidload (define ticker globally).
                    for (TFHppleElement *element in array)
                    {
                        ticker.marqueeStr = [[element firstChild] content];
                    }
                    ticker.marqueeFont = [UIFont boldSystemFontOfSize:22];
                    [self.view addSubview:ticker];
                    NSLog(@"bla bla");
                }
            }