一、使用計時器調用一個類方法
1、計時器只能調用實例方法凌停,但是可以在實例方法中調用類方法(包一層的方法在開發(fā)中有很多妙用),代碼示例如下:
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
-(void)timerMethod
{
//調用類方法
[[self class] staticMethod];
}
-(void)invalid
{
[timer invalid];
timer = nil;
}
2售滤、使用NSTimer時要注意罚拟, 計時器一定要加入Runloop中,并且選好mode才能執(zhí)行完箩,scheduledTimerWithTimeInterval方法創(chuàng)建一個計時器并加入到RunLoop中所以可以直接使用赐俗。
3、如果定時器的repeats設置為YES弊知,說明定時器會重復執(zhí)行阻逮, 一定要在合適的時機invalid。 (注意不能在dealloc中調用invalid秩彤, 因為一旦repeats設置為YES叔扼, 計時器NSTimer就會強只有self,導致dealloc永遠不會調用漫雷,這個類也就不會被釋放瓜富,比如我們可以在viewDidDisappear中調用invalid,這樣就可以正常執(zhí)行dealloc并釋放了)