Re: Changing Subview Order Prevents Dragging
Re: Changing Subview Order Prevents Dragging
- Subject: Re: Changing Subview Order Prevents Dragging
- From: Quincey Morris <email@hidden>
- Date: Mon, 13 May 2013 20:38:28 -0700
On May 13, 2013, at 19:41 , Graham Cox <email@hidden> wrote:
> When you call -removeFromSuperview, the view is deleted, as there are no more references to it. The other methods are not called because the object ceases to exist.
I believe your warning is apposite, but is not actually the cause of Tom's problem. After removing the view, his code adds it back again. If the view had already been deallocated, you'd expect a pretty big crash pretty soon (though not necessarily immediately).
My guess is that the removed view is still retained by something, but that removing a view from its window causes the mouse-dragging state machine to be reset. If so, immediately re-adding the view isn't going to restore the dragging state.
I think the trick is to avoid removing the view from its superview. I'm not sure of the *best* way of doing this, but here are the things I'd try:
-- Invoke 'addSubview: self' without first removing self. The NSView machinery may be clever enough merely to move the view to the end of the subviews array, without actually removing and re-adding it.
-- Create a mutable copy of 'self.superview.subviews', move 'self' to the end of the mutable copy, then use 'self.superview setSubviews:' to establish the new order. Note that using 'setSubviews:' this way is explicitly called out in the NSView documentation as a way of reordering views.
-- Use 'sortSubviewsUsingFunction:context:' with a suitable comparison function to move 'self' to the end of the subviews.
Of course, it's still possible that any or all of the above will reset the dragging state, but they're certainly worth a try.
P.S. The OP's issue is one reason why using "many small, draggable views" probably isn't a good design solution to the problem anyway.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden