Pregunta

I have a time that repeats itself every .1 seconds (I do this because I need to constantly look for a change on the website's text I am pulling from). My problem is that I need to be able to tell when the text changes from one word to another. So when the user open the app and let's say the text is displaying one song title but then it changes to a different one, Where it changes, I need to be able to detect it and preform an action.

Also it has to be when the app first loads it will not preform the action but when the text changes it will display it(It has to be this because I am pulling song titles). So I need to be able to change the duration of each song, but if a song is playing and they open the app in the middle of it, the duration will be mixed up. so I have two labels, one that shows text and another that shows the time. When the app first loads up, I want it to display nothing until the text in the song title changes, then pulls the duration from a if statement (below). It might be a little confusing but ask any question you need and I will try to explain.

Here is my code for pulling from the website and anything else that might help:

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

-(void)recentTracksText {

    textForBlog = [webViewForRecents stringByEvaluatingJavaScriptFromString:@"document.getElementById('current_song').textContent;"];

    self.strippedTextForBlog = [self stringByStrippingHTMLFromString:textForBlog];

    continuousLabel.text = self.strippedTextForBlog;

}

if ([continuousLabel.text isEqual: @"Lady Gaga - Gypsy"]) {
            imageView1.image = [UIImage imageNamed:@"Lady Gaga - Gypsy.jpg"];
            imageView.hidden = YES;
            [self.view insertSubview:toolbar belowSubview:imageView];
            [toolbar insertSubview:darkView belowSubview:imageView];
            [self.view insertSubview:backgroundImage belowSubview:toolbar];
            if([darkView.subviews containsObject:continuousLabel]) {

            } else{
                dispatch_queue_t queue = dispatch_get_global_queue(0,0);
                dispatch_async(queue, ^{
                    [darkView addSubview:Label];
                    [Label setText:@"1:30"];
                    [darkView addSubview:continuousLabel];
                });
            }

        } 

UPDATE

-(void)recentTracksText {

    textForBlog = [webViewForRecents stringByEvaluatingJavaScriptFromString:@"document.getElementById('current_song').textContent;"];

    self.strippedTextForBlog = [self stringByStrippingHTMLFromString:textForBlog];
    if (![self.strippedTextForBlog isEqualToString:continuousLabel.text])// this does not find when the text changes {
        [self.pLabel setHidden:NO];
        [self.pLabel setProgress:1  
                          timing:TPPropertyAnimationTimingEaseOut
                        duration:150.0
                           delay:0.0];  //This does not go smoothy.
    // this does not 
    }  
    continuousLabel.text = self.strippedTextForBlog;
}
¿Fue útil?

Solución 2

Well, you already have the NSTimer that fires each 0.1 seconds, so you just have to check if the text wasn't changed.

-(void)recentTracksText {

    textForBlog = [webViewForRecents stringByEvaluatingJavaScriptFromString:@"document.getElementById('current_song').textContent;"];

    self.strippedTextForBlog = [self stringByStrippingHTMLFromString:textForBlog];

    if (![self.self.strippedTextForBlog isEqualToString:continuousLabel.text]) {
    //The text isn't the same. Do something.
    }

    continuousLabel.text = self.strippedTextForBlog;

}

Otros consejos

Use Key-value observing if you want to observe the property directly

[continuousLabel addObserver:self
         forKeyPath:@"text"
            options:0
            context:NULL];

Make sure you implement the observing method

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
   // Insert your code here to deal with the change.
}

and a little bit of ARC management to taste:

- (void)dealloc {
    NSLog(@"dealloc %@", [self class]);
    if (continuousLabel) {
        [continuousLabel removeObserver:self forKeyPath:@"text"];
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top