是否可以在iOS中调用某个函数一定的时间间隔?在iOS中每5秒钟调用一次函数
{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(serverConnect) userInfo:nil repeats:YES];
}
有没有其他替代方案?
服务器连接功能如下:
-(void)serverConnect{
NSMutableURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@MYURL] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15.0];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(connection){
NSLog(@"connected");
}else{
//
}
它引发错误: - [TUTViewController serverConnect:]:无法识别的选择发送到实例0x907ef20
这几乎是做到这一点。全球化'NSTimer',你可以在其他地方无效,如果你需要的话。 – MobileOverlord 2012-02-01 01:52:28
这就是你如何做一个重复计时器,但它会每隔100秒运行一次。这是你真正想要的吗? – jsd 2012-02-01 01:52:28
我改变了时间间隔并添加了被调用的函数。 – 2012-02-01 02:01:04