如何在Xcode中每分钟重新加载一次视图?

时间:2013-03-15 10:11:35

标签: xcode parsing view radio

我有以下问题 - 我有一个收音机应用程序。我正在从Web HTML元素解析当前歌曲的标题并将其显示在标签中。我正在使用NSTimer在我的函数中每分钟加载此元素,以查看它是否已被更改。这是我的功能:

-(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");
    }
}

在我的viewDidLoad中我这样做:

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

}

以这种方式,标签出现在旧标签上的每一分钟,而不是替换它。如何用新的替换我正在显示的旧视图([self.view addSubview:ticker];)?

1 个答案:

答案 0 :(得分:1)

一旦推荐这个

     -(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");
            }
        }