tags:

views:

113

answers:

3

Hi,

I have a super view, which has 2 subviews. These subviews are overlapped.

Whenever i choose a view from a menu, corresponding view should become the front view. i.e., it should be the font most subview.

acceptsFirswtResponder, resigns all work fine. But the mouse down events are sent to the topmost sub view which was set.

Regards, Dhana

A: 

Below given code should work fine..

    NSMutableArray *subvies = [NSMutableArray arrayWithArray:[self subviews]];//Get all subviews..

    [viewToBeMadeForemost retain]; //Retain the view to be made top view..

    [subvies removeObject:viewToBeMadeForemost];//remove it from array

    [subvies addObject:viewToBeMadeForemost];//add as last item

    [self setSubviews:subvies];//set the new array..
Dhanaraj
+1  A: 

Here's another way to accomplish this that's a bit more clear and succinct:

[viewToBeMadeForemost removeFromSuperview];
[self addSubview:viewToBeMadeForemost positioned:NSWindowAbove relativeTo:nil];

Per the documentation for this method, when you use relativeTo:nil the view is added above (or below, with NSWindowBelow) all of its siblings.

Adam Preble
+1  A: 

But beware that "Note: For performance reasons, Cocoa does not enforce clipping among sibling views or guarantee correct invalidation and drawing behavior when sibling views overlap. If you want a view to be drawn in front of another view, you should make the front view a subview (or descendant) of the rear view."

(read the View Programming Guide in the oxs doco)

Joe Blow