2011-07-21 64 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: 

消息被觸發到選擇器。谷歌上有很多示例代碼。