Q: I have a button which, when clicked, removes a View from a Window. However the button that does this is a subview of the View that is removed. This works except I get the following message on the console:
Assertion failed: You removed a View from the View hierarchy that had been lockFocus'ed
Is this a problem? Can I ignore the message?
A: Typically the action method for a control is called while in the mouseDown for that control. At this point the AppKit has performed a lockFocus on that control (while it is highlighted) and it will not remove the lockFocus until the action method has completed (and the highlight is turned off). For this reason it is not a good idea to be modifying the view hierarchy of any superviews from within the action method of a control. If it is not possible to restructure the code so that the hierarchy is modified outside of the action method, then the next best approach is to perform the action with a slight delay. The code might look like:
[self perform:@selector(removeView:) with:sender afterDelay:1 cancelPrevious:YES];
There is also an example of this method in the DrawApp class of the Draw example.
QA856
Valid for 1.0, 2.0, 3.0