2011-07-21 30 views
0

我有pickerview,它在(5,10,20)等minuts中显示时间。如果用户选择任何时间,他将在选定的时间之后获得更新。而我的应用程序将照常运行,但5分钟后会显示一些消息。如何在iphone应用程序中实现“检查更新”概念

我将如何实现这个概念?我应该做什么编码?

根据我的猜测,我应该使用线程吗?

回答

1

你可以有一个NSTimer,当你从你的pickerview中选择一个时间时开始。

timer = [NSTimer scheduledTimerWithTimeInterval:pickerValueInSeconds target:self selector:@selector(updateMethod) userInfo:nil repeats:NO]; 

您选择不重复,那么在你的updateMethod你提出一个要求:

-(void)updateMethod 
{ 
    NSString *url = @"url..."; 
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:someURL] 
             cachePolicy:NSURLRequestUseProtocolCachePolicy 
              timeoutInterval:20.0]; // with some timeout interval. 

    // create the connection with the request 
    // and start loading the data 
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

    if(theConnection) 
    { 
     // Create the NSMutableData to hold the received data. 
     // receivedData is an instance variable declared elsewhere. 
     systemsData = [[NSMutableData data] retain]; 
    } 
    else 
    { 
     // Inform the user that the connection failed. 
     NSLog(@"NSURLConnection failed!"); 
    } 

} 

现在你

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 

你照顾数据或错误,然后启动一个EW定时器..

请注意,您还需要实现

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [systemsData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [systemsData appendData:data]; 
} 
+0

很好的工作。谢谢 –

0

您可以创建一个NSTimer,等待选择的持续时间。一旦超时完成,

scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: 

消息被触发到选择器。谷歌上有很多示例代码。