« Q&A: May 26, 2001

Posted by Andy Monitzer on May 26, 2001 [Feedback (0) & TrackBack (0)]

// Question One

Is it possible to include an image file in the nib file and load it programmatically into a NSImage to be displayed in a NSImageView?


Put the image in your project (putting images directly into nibs was possible in the past, but it is no longer allowed/encouraged). Then, in the NSImageView, put the name of the file into the "icon" attribute in the inspector. (minus the ".tiff" extension)

answer by: Dan Wood

// Question Two

Is it possible to make my project in PB break whenever a NSException is raised?


at the gdb prompt type

b -[NSException raise]

answer by: Henri Lamiraux

If you want to break on something that is not yet loaded, use "fb" rather than "b".

answer by: Douglas Davidson

// Question Three

Has anybody figured out how to change the Cocoa font antialiasing threshold?

Before setting up and running your NSApplication instance, set the defaults "AppleScreenAdvanceThreshold" and "AppleSmoothFontSizeThreshold" to an interger for the minimum size.

answer by: Rick Roe

// Question Four

I'm looking for the best way to determine if a process known by PID exists.

if (kill(pid,0) < 0) {
	/* process does not exist */
} else {
	/* Process does exit now i can do something */

answer by: Vince DeMarco

// Question Five

I have a need to create small controls programmatically. The problem I have is that for many a control, there's no 'small' property in the class itself (or a setControlSize function).


Use setControlSize: with NSSmallControlSize (I think these are defined in NSCell.h, not sure though). For some you have to do setControlSize: on the cell instead of the control itself (those that have cells...)

answer by: Chuck Pisula

// Question Six

Could someone tell me the difference between a delegate and a controller?


A delegate is an object which allows you alter the behavior of a class without subclassing by implementing certain methods which rely on notifications posted by the delegating object. While a controller is an object which coordinates messaging between the model object(s) and the view object(s).

answer by: David P Henderson

// Question Seven

So, let's say that I've got a window in a NIB. It's not a document-based app, but I want to create a second window using exactly the same attributes from the same NIB as the main window.


I would strongly suggest that, if you will have more than one copy of a window, that it is in a separate NIB file from the MainMenu nib.

If you are using the standard, non-subclassed version of a NSWindowController, then do this:

NSWindowController *myWindow = [[NSWindowController alloc]

if you've subclassed NSWindowController:

self = [super initWithWindowNibName:@"nibNameMinusExt" owner:self];

Remember, when you do the above NSBundle is loading the named nib, thus any objects instantiated in the nib will also be loaded (which is what is supposed to happen). That is why you want your window in a nib other than the main nib file.

answer by: Chris Behm

// Question Eight

From what I've seen, a NSMenuItem in a NSMenu in a the menu bar when it is created in IB is disabled as long as there is no Action/Target linked to it.

As soon as you create this link, the NSMenuItem is enabled. Then could someone explain me why when you do a [thisAnnoyingMenuItem setEnabled:NO]; it stays enabled ?


By default, the menu has autoenableItems set to YES. This means that before tracking in the menu begins, it enables all items that have target/action and then for each item, it calls validateUserInterfaceItem: on the target and then up the responder chain. If none of the objects in the chain this implements this method, it assumes the item should always be enabled. If you want to disable the item, implement this method and have it return NO.

Usually, it's something like:

- (BOOL)validateUserInterfaceItem:(id)anItem {
     SEL action = [anItem action];
     if (action==@selector(myAction:)) {
         return [self myActionEnabled];
     return NO;	// disabled

answer by: Andrew Platzer

Post a comment