Mobile/iOS

[iOS] 일정시간 이후에 Method 호출하기

out of coding 2016. 8. 1. 18:25

많은 사람들이 알 내용이다.

뭐 그래도 모르는 사람들은 잘 이용하길 바랍니다.



performSelector:withObject:afterDelay:


* 사용방법

SEL sMethod = @selector(TestMethod);

if([self respondsToSelector:sMethod] == YES) 

[self performSelector:sMethod withObject:nil afterDelay:0.1];


이런식으로 메소드를 호출하는 경우 현재 스레드와 동일한 스레드 내에서 실행이 됩니다.


이렇게 일정시간 이후 실행되는 메소드의 호출을 취소하고 싶은 경우에는 다음을 사용


[NSRunLoop cancelPreviousPerformRequestsWithTarget:self]; 


이렇게 하면 가장 마지막에 던진 메소드가 취소됩니다.

이렇게 하지 않고 특정 메소드를 취소시키려면 Selector를 지정해주면 됩니다. 


[NSRunLoop cancelPreviousPerformRequestsWithTarget:self selector:sMethod object:nil];