Iphone 在一段时间间隔后更新UIView?

Iphone 在一段时间间隔后更新UIView?,iphone,ios,ipad,uikit,Iphone,Ios,Ipad,Uikit,我想创建自定义视图,在一段时间后调用webservice并自行更新。即使应用程序未处于活动状态,也应更新数据。。那么,做这件事的最佳方法是什么呢?您可以使用NSTimer。在自定义方法中添加代码,并像下面那样调用该方法 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(yourMethodName:) userInfo:nil repeats:YES];

我想创建自定义视图,在一段时间后调用webservice并自行更新。即使应用程序未处于活动状态,也应更新数据。。那么,做这件事的最佳方法是什么呢?

您可以使用
NSTimer
。在自定义方法中添加代码,并像下面那样调用该方法

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(yourMethodName:) userInfo:nil repeats:YES];
在这里的
scheduledTimerWithTimeInterval
参数中,您可以设置以秒为单位的时间,以便每隔3秒调用传入
selector
参数的方法

请参见此链接中的
NSTimer
示例和教程

更新:

如果您想调用webservice,那么可以像下面那样使用
NSThread

- (void) runTimer 
{
    [NSThread detachNewThreadSelector:@selector(updateAllVisibleElements)toTarget:self withObject:nil]; 
}

- (void) updateAllVisibleElements  {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    if(service == nil)
    {
        service = [[WebService alloc] init];
    }
    [service getlocationID:currentLatitude andlongitude:currentLongitute];
    [pool release];
}

请参阅链接

当应用程序处于后台时,您可以跟踪位置,但无法连接到web服务。

使用
NSTimer
,但当应用程序处于后台模式时,数据不会更新。应用程序激活后,
NSTimer
将继续工作。

您自己尝试过任何解决方案吗?请尝试此链接:@MarcinKuptel l已与NSTimer合作过。。但正如我所说,即使应用程序在后台,我也要更新数据。我不知道这一点。你能在后台调用webservice吗?在这个问题上,他还提到在后台调用webservice也没有,因为你可以在这里看到答案和答案