使用NSTimer从服务器刷新数据

时间:2016-02-16 05:17:48

标签: ios objective-c xcode6 nstimer

我想从服务器刷新数据,但是刷新时间 - >我想从用户那里得到它并传递给计时器,我怎么能这样做我知道给出的价值,如

self.timerCounter =[NSTimer scheduledTimerWithTimeInterval:300.0  
target:self selector:@selector(getGraphData) userInfo:nil repeats:YES];

我也尝试使用协议和代表,如

self.timerCounter =[NSTimer scheduledTimerWithTimeInterval:strTime
target:self selector:@selector(getGraphData) userInfo:nil repeats:YES];

但我得到了错误

  

将NSString _strong发送到不兼容类型的参数   NSTimerInterval

现在,我想从文本域中的用户那里得到它,请帮我做这个

3 个答案:

答案 0 :(得分:1)

textfield.text转换为NSTimeInterval 试试吧, let strTime : NSTimeInterval = NSTimeInterval(textfield.text)!

答案 1 :(得分:1)

您使用的是NSString而非NSTimeInterval类型的数据。您应该可以通过将txtTimestrTime转换为double值来解决此问题。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *url = [defaults objectForKey:@"server_url"];
NSString *txtTime = [defaults objectForKey:@"refresh_time"];

if (txtTime != nil) {
    NSTimeInterval timeInterval = [txtTime doubleValue];
    self.timerCounter = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(getGraphData) userInfo:nil repeats:true];
}

答案 2 :(得分:1)

我认为将strTime转换为NSTimeInterval存在问题。

self.timerCounter =[NSTimer scheduledTimerWithTimeInterval:[strTime doubleValue] target:self selector:@selector(getGraphData) userInfo:nil repeats:YES];

试试这个,如果存在转换问题,这应该有效。