2012-05-15 12 views
11

可能性の重複:定期的に特定の関数を呼び出す方法
How to call function in every “X” minute?どのように自動着信機能にすべてのX時間

- (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]; 

    }  
} 

はどのようにしておきx分または秒[self checkAlert];呼び出すことができますか?

答えて

25

[NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(checkAlert) userInfo:nil repeats:YES];を使用してください。

+0

ああ、次のようになります....すごい...それはありがとうが私のために働いていないjimpic – Vasuta

+0

をverymuch ...仕事です.. – shim

+0

make repeat = NOではなく、より良い出力が得られます。ありがとう –

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

...そして、あなたのmyTimerTick:方法は...

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

ああ、おかあさんverymuch – Vasuta

+1

これは動作します。まだ2016年です – Paul

関連する問題