11

重複の可能性:
「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];

    }    
}

分または秒[self checkAlert];ごとに電話をかけるにはどうすればよいですか?x

4

2 に答える 2

26

を使用し[NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(checkAlert) userInfo:nil repeats:YES];ます。

于 2012-05-15T09:22:16.723 に答える
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.
    }
}
于 2012-05-15T09:27:21.603 に答える