2012-05-15 44 views
11

thể trùng lặp:
How to call function in every “X” minute?Làm thế nào để gọi chức năng tự động mỗi khi x

Làm thế nào để định kỳ gọi một chức năng nhất định?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self checkAlert]; 
} 



-(void)checkAlert{ 
    // Server output Alert Note 
    NSString *alert_note_hostStr = @"http://www.loxleyintranet.com/MobileApplication/xml/alert_note.php"; 
    NSData *alert_note_dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:alert_note_hostStr]]; 
    NSString *alert_note_serverOutput = [[NSString alloc] initWithData:alert_note_dataURL encoding:NSASCIIStringEncoding]; 

    if ([alert_note_serverOutput isEqualToString:@"0"]) { 
     alertImage.hidden = YES; 
     alertLabel.hidden = YES; 
     underMainBG.hidden = YES; 
     alertLabel.text = [NSString stringWithFormat:@"No Alert"]; 
    }else{  
     alertLabel.text = [NSString stringWithFormat:@"You've Got Note (%@)" ,alert_note_serverOutput]; 

    }  
} 

Tôi làm cách nào để gọi [self checkAlert]; mỗi x phút hoặc giây?

Trả lời

25

Sử dụng [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(checkAlert) userInfo:nil repeats:YES];.

+0

Oh .... wow ... đó là công việc ... thankyou verymuch jimpic – Vasuta

+0

Không làm việc cho tôi .. – shim

+0

lặp lại = NO, thay vì có đầu ra tốt hơn. Cảm ơn –

10
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myTimerTick:) userInfo:nil repeats:YES]; // the interval is in seconds... 

... thì phương pháp myTimerTick: của bạn sẽ giống như thế này ..

-(void)myTimerTick:(NSTimer *)timer 
{ 
    if(some_contiditon) 
    { 
     // Do stuff... 
    } 
    else 
    { 
     [timer invalidate]; //to stop and invalidate the timer. 
    } 
} 
+0

oh thankyou verymuch – Vasuta

+1

Tác phẩm này. Vẫn còn trong năm 2016. – Paul

Các vấn đề liên quan