开发者

Strange error with NSTimer (I believe)

开发者 https://www.devze.com 2023-04-05 09:10 出处:网络
I\'m making an app where I use an NSTimer to make a loop. When the app starts, in viewDidAppear I check if a certain value in NSUserDefaults is set, and if it\'s not I show the user an UIAlertView wit

I'm making an app where I use an NSTimer to make a loop. When the app starts, in viewDidAppear I check if a certain value in NSUserDefaults is set, and if it's not I show the user an UIAlertView with an UITextView. Until here all ok. Once the user sets the user defaults value, the alert view's alertView:clickedButtonAtIndex: calls a void that makes the timer work:

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [standardUserDefaults setObject:nameField.text forKey:@"host"];
    [standardUserDefaults synchronize];
    [self setUp];
    NSLog(@"called");
}

This is [self setUp];

-(void)setUp {


    timer =开发者_JAVA百科 [NSTimer timerWithTimeInterval: 1  target: self selector: @selector(reloadData) userInfo: nil repeats: YES];

    [[NSRunLoop mainRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode];
    [self reloadData];
    self.host = [self getHostName:val];
    self.ip = [self getHostIP:val];
    NSLog(@"reached here"); //THIS GETS CALLED SO I THINK IT WORKS


}

This is [self reloadData]:

- (void)reloadData {
    NSArray *words = nil;
    words = [[self getUptime] componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    //int sum = [[words objectAtIndex:0] intValue] / 100.0;
    float sum1 = ([[words objectAtIndex:0] floatValue] / 10.0);
    NSLog(@"%f", sum1);
    [bar1 setProgress:sum1];
    load1.text = [NSString stringWithFormat:@"%f", [bar1 progress]];

    float sum2 = ([[words objectAtIndex:1] floatValue] / 10.0);
    NSLog(@"%f", sum2);
    [bar2 setProgress:sum2];
    load2.text = [NSString stringWithFormat:@"%f", [bar2 progress]];


    float sum3 = ([[words objectAtIndex:2] floatValue] / 10.0);
    NSLog(@"%f", sum3);
    [bar3 setProgress:sum3];
    load3.text = [NSString stringWithFormat:@"%f", [bar3 progress]];

}

The problem is that the tableView cells doesn't get the values and remains white, but if you close the app and load it again (when it has the variable in the user defaults) the thing works perfectly so I don't know what could happen. I also get so often wait_fences: failed to receive reply: 10004003in the log (I know this happens when you don't call the superview)


Do you call [table reloadData]? It's odd that you named your own internal function the same as the tableview's message.


duh, I found the problem. I was using the variable val which was set on app start, and it was empty when the user defaults didn't had that key :/ so I just did: NSString *userhost = [standardUserDefaults objectForKey:@"host"]; when calling the setUp void after dismissing the alert view

0

精彩评论

暂无评论...
验证码 换一张
取 消