First time I'm writing something here. I have a question that i hope someone can give me an answer to.
I'm developing a screen saver for Mac OS X Lion using Objective-C. I have read some tutorials on how to do using a ScreenSaverView. Everything works perfect until I plug in another monitor. The program then run all the methods twice (loads the xml-file twice) and stuff like that. Whats the solution to this?
Should I use something else than ScreenSaverView?
Thanks!
I usually solve this by using a singleton (global variable initialed to true):
static BOOL gFirst = YES;
Then in the class initialization method I save it's value to an ivar & then set the global false:
- (id) initWithFrame: (NSRect) frameRect isPreview: (BOOL) isPreview {
preview = first = NO; // assume failure (pessimist!)
if ( isPreview ) {
preview = YES; // remember this is the preview instance
} else if ( gFirst ) { // if the singleton is still true
gFirst = NO; // clear the singleton
first = YES; // and set the ivar
}
Note: I also save the isPreview flag to an ivar and don't clear/set the first global/ivar when it's true. So the logic here will set preview to true when isPreview is true otherwise will set first true the first time this class is instantiated (when isPreview is false) and false otherwise.
Now in the drawRect I use the preview & first ivar's to affect what get's drawn…:
- (void) drawRect: (NSRect) inRect {
if ( preview || first ) {
// ADD THE DRAWING CODE FOR THE 1ST MONITOR (or preview view) HERE
} else {
// ADD THE DRAWING CODE FOR THE NTH MONITOR(S) HERE
[super drawRect:inRect]; // draw the default black background
}
}
Note: Some screen savers do their drawing in their animateOneFrame method so you may need to move the above preview/first logic there.