Question

I have a UIView subclass, in some cases I need to change its color for some period, say 5 seconds, then change the color back, the method I used is performSelector:afterDelay: and the selector I passed in is to change the color back, I was wondering if the view is removed from superview, before the color changing method is fired, then does it cause any problem? Or it is just not called as the view object itself does not exist?

Thanks!

Was it helpful?

Solution

When you send performSelector:withObject:afterDelay: to your view, the view essentially creates a timer. The timer retains the view, and the argument object (presumably a UIColor in your case). So even if you remove the view from its superview, the timer still retains the view and prevents it from being deallocated.

When the timer fires, it will change the (off-screen) view's background color, then release the view. If that was the last strong reference to the view, the view will then be deallocated.

When you change the background color of a view that's not in a window hierarchy, the view just remembers its new background color setting, but it doesn't do anything else like try to draw itself on the screen.

So using performSelector:withObject:afterDelay: to change your view's background color should not cause any problems even if you remove the view from its superview before the delay expires.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top