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 > "Hide" a view in Obj-C

"Hide" a view in Obj-C
Thread Tools
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Dec 24, 2001, 09:00 PM
 
Hello ...

I want to be able to hide and show a view (a text field, actually). I believe AppleScript Studio allows this the same way HyperCard used to: "set the visible [of the object] to false|true". What about Obj-C?


Relatedly, but more out of curiosity: In the OS X login window (among other places), UI elements fade and move to different positions. The moving I can emulate, the fading I can't figure out (without sub-classing NSButton, etc, and overriding the drawRect: method; the .nibs for the loginwindow.app, however, show only un-sub-classed NSButtons).

How is this done?


Thanks and regards,

DayLateDon
     
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
Dec 24, 2001, 11:44 PM
 
I'd be sort of surprised to find an NSView / NSControl hiding mechanism available in AppleScript Studio, since it isn't available via Obj-C or Java. NeXT/Apple never implemented it because controls that hide and show themselves are generally a Bad Thing in Human Interface terms. (Granted, there are several legitimate applications for hiding controls, but not near as many as developers tend to think.)

If it's a non-editable text field (that is, a non-bordered label) you want to hide, consider just setting its stringValue to @"". If it's an edit field, why not just disable it? Then it won't surprise the user by appearing and disappearing unexpectedly.

However, this is a frequently asked question in Cocoa developer circles, and as such, there are a few common ways to tackle the problem.

The simplest is to use -removeFromSuperview on the NSView you want to hide. Since removing a view from the hierarchy releases it, you'll want to make sure you retain it first so it doesn't get deallocated before you want to show it again (by using -addSubview . This doesn't work so well for auto-resizing of views and subviews, though, since your view will come back in with its old frame when you add it back again.

Another way that's been suggested is to create a trivial subclass of NSView which implements hiding by overriding -drawRect: to be a no-op whenever the view is supposed to be hidden. Make the subclass poseAs: NSView, and you can then hide any view or control. Of course, writing a posing subclass has to be careful work...

As for the fading: I believe if you look in the open-source OmniAppKit framework (available from our web site) you'll find some code for fading views in and out when adding or removing them from the view hierarchy.
Rick Roe
icons.cx | weblog
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Dec 25, 2001, 12:05 AM
 
Hello ...

I'd be sort of surprised to find an NSView / NSControl hiding mechanism available in AppleScript Studio, since it isn't available via Obj-C or Java.
Surprise! From the Drag Race example (in a variety of places):

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre>& amp;lt;font size=1 face=courier>
set visible of progress indicator <font color = red>"ProgressBar"</font> to false
</font></pre><HR></BLOCKQUOTE>

So, there _is_ an AppleScript mechanism (and it works!). Hmmmm ... What's the easiest way of rolling a single line of AppleScript into a Cocoa project?

Edit: The mechanism works to hide the progress bar, but when I alter the line to refer to one of the text fields, I get "NSReceiverEvaluationError"s. Weird.


Thanks for the pointer on removeFromSuperview and the Omni frameworks.


Regards,

DayLateDon

[ 12-26-2001: Message edited by: DayLateDon ]
     
   
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:10 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2