3

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)

  • are you adding the label to a main visible view? – Nekak Kinich Oct 29 '13 at 23:41
  • 2
    Are you sure it's running on the main thread? – Kevin Oct 29 '13 at 23:43
  • I want to update the label inside of a instance method called connection –  Oct 29 '13 at 23:53
  • Have you set up an outlet for your `returnInfo` label in you view controller? Because it does not look like a property and in your ViewController.m file I cannot see where you connect the variable `returnInfo` with the actual label. – Mischa Oct 30 '13 at 00:06
  • it is connected (maybe?) using the visual interface of dragging from the connections inspector to the label –  Oct 30 '13 at 00:11
  • Do you use `needsLayout`? – Maen Oct 30 '13 at 00:20

1 Answers1

1

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.

Community
  • 1
  • 1
Vlad Z.
  • 3,401
  • 3
  • 32
  • 60