目前我正在創建一個NSTimer,每秒調用一次我的方法。如何停止我的NSTimer線程進一步執行選擇器
我這樣做像這樣:
[NSTimer scheduledTimerWithInterval:1 target:self selector:@selector(clocktick) userInfo:nil repeats:YES];
我怎麼能阻止這從打電話clocktick
?
我試過像這樣分配它:NSTimer *myTimer = [NSTimer ...
,然後使用myTimer = nil;
但是什麼也沒做。
你可以創建一個私有屬性來保存對類中定時器的引用。或者讓您正在調用的函數接受NSTimer *作爲參數。這樣你可以取消定時器每秒調用的函數中的定時器。 (例如: - (void)targetMethod:(NSTimer *)theTimer {...}) – blueyond