Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Layering NSViews - send to back?

Layering NSViews - send to back?
Thread Tools
Brass
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Dec 16, 2002, 06:52 PM
 
How to I tell an NSView to go to the back?

It seems that some NSViews pop to the front whenever I do anything to them, so I need to either stop this behaviour, or else tell them to go to the back after I do anything with them.
     
Detrius
Professional Poster
Join Date: Apr 2001
Location: Asheville, NC
Status: Offline
Reply With Quote
Dec 18, 2002, 01:55 AM
 
Try looking at the NSResponder class.

If that doesn't help any, you may need to find a way to structure an NSView hierarchy, with subviews and all.
ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
Brass  (op)
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Dec 18, 2002, 05:09 PM
 
Originally posted by Detrius:
Try looking at the NSResponder class.

If that doesn't help any, you may need to find a way to structure an NSView hierarchy, with subviews and all.
I couldn't see anything in NSResponder. I also tried using the NSView method (can't remember what it's called now) to order all it's subviews, but couldn't get it to work correctly either (I guess I missunderstood how to use it?). But that wasn't really what I wanted anyway. It's a bit of overkill to re-order everything when all I wanted to do was to move one subview to the back.

In any case, I've found a dodgy work around which doesn't require me to re-order things.
     
IamBob
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
Dec 18, 2002, 11:11 PM
 
If you're using IB to set up your views this should be pretty trivial.

Else, I'd suggest looking at NSView's addSubview: positioned: relativeTo:...

Code:
{ id contentView = [window contentView]; id backView; // assume this exists [backView retain]; [backView removeFromSuperview]; [contentView addSubview:backView positioned:NSWindowBelow relativeTo:nil]; }
That should remove backView from the contentView and put it back - below everything else.

If that doesn't work... heh, HTH!

Edit;
I figured I could do one better...

NSView(Additions).h...
Code:
#import <AppKit/AppKit.h> @interface NSView (Additions) - (void)sendToBack; @end
NSView(Additions).m...
Code:
#import "NSView(Additions).h" @implementation NSView (Additions) - (void)sendToBack { id superView = [self superview]; if (!superView) return; // can't do anything [self retain]; [self removeFromSuperview]; [superView addSubview:self positioned:NSWindowBelow relativeTo:nil]; [self release]; } @end
[yourView sendToBack];

Does it work!?

edit; added the balancing release message
( Last edited by IamBob; Dec 21, 2002 at 11:51 AM. )
     
Detrius
Professional Poster
Join Date: Apr 2001
Location: Asheville, NC
Status: Offline
Reply With Quote
Dec 18, 2002, 11:47 PM
 
You have an unbalanced retain/release pair in there. The object gets retained every time you send it to the back, without ever having that retain counteracted. You should release at the end. (unless there is a similar unbalanced retain/release pair in Apple's code, which I doubt)
ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
IamBob
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
Dec 21, 2002, 11:47 AM
 
edit; d'oh! you're right, it should be released after adding it to the view. my bad!
( Last edited by IamBob; Dec 21, 2002 at 11:54 AM. )
     
Mac Boy
Junior Member
Join Date: Dec 2000
Location: Boston, MA USA
Status: Offline
Reply With Quote
Dec 21, 2002, 01:03 PM
 
I looked into layering subviews while working on a multimedia creation app (which has since been pretty much abandoned, but who knows) and found a note somewhere in the docs that said never to do this. The NSView architecture was never designed to layer multiple views on top of each other. The gist of it I guess is that it usually works fine, but can be unreliable, and you basically come up with just a hack to get them to work.

My solution to this problem was just to design my own class structure. Rather than using NSViews, my classes had similar interfaces but were much more specialized. A "viewer" NSView would go through an array of these objects and draw them, to be sure they always drew back-to-front, and they also were much lighter than NSViews which made my app quite a bit faster.

Just something you might want to consider if this is a larger project dependent on layered views.
     
Misha Opanasyuk
Fresh-Faced Recruit
Join Date: Sep 2012
Status: Offline
Reply With Quote
Sep 13, 2012, 01:25 AM
 
You wrote that post in 2002? it helped me in 2012! THNX!
Hello from futureXD
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 09:06 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,