user2934227
user2934227

Reputation:

Refresh a UILabel

I am trying to update a UILabel using code in the ViewController.m file.

returnInfo.text = @"The website is up!";

but this change does not appear in the app for up to a few minutes but it does appear . What am I doing wrong and why does it take so long to appear? I want the label to visually update as soon as this code executes.

This is my entire ViewController.m file: http://pste.co/p/mTvcgTftdr1a (lines 40, 45, 51)

Upvotes: 3

Views: 806

Answers (1)

Vlad Z.
Vlad Z.

Reputation: 3451

I have a really dirty(crappy) solution, it's just i can't think about better one right now.

Solution is next:

ViewController.h

NSString *someData;
NSTimer *testTimer;

ViewController.m

-(void)tempMethod{
   if (([someData rangeOfString:@"\"status_code\": 1"].location == NSNotFound) &&
                    ([someData rangeOfString:@"\"status_code\": 2"].location == NSNotFound)) {
                returnInfo.text = @"This URL looks invalid";
                NSLog(@"Status Code 3");

            } else if (([someData rangeOfString:@"\"status_code\": 2"].location == NSNotFound) &&
                        ([someData rangeOfString:@"\"status_code\": 3"].location == NSNotFound))  {
                returnInfo.text = @"The website appears to be up!";
                [returnInfo setNeedsDisplay];
                NSLog(@"Status Code 1");
            } else if (([someData rangeOfString:@"\"status_code\": 3"].location == NSNotFound) &&
                        ([someData rangeOfString:@"\"status_code\": 1"].location == NSNotFound)) {
                NSLog(@"Status Code 2");
                returnInfo.text = @"This website is down";
            }
}

- (IBAction)connection:(UIButton *)sender{
    NSString *url= @"http://isitup.org/duckduckgo.com.json";
    NSURLSession *session = [NSURLSession sharedSession];
    [[session dataTaskWithURL:[NSURL URLWithString:url]
            completionHandler:^(NSData *data,
                                NSURLResponse *response,
                                NSError *error) {
                someData = [NSString stringWithUTF8String:[data bytes]];
                NSLog(@"%@",someData);
            }] resume];
    testTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(tempMethod) userInfo:nil repeats:YES];
}

My advice to you is next: read about UIViewController life cycle and threading:

Another sorry for dirty code. I can take few minuses for that answer, i'll think about better one tomorrow.

Upvotes: 1

Related Questions