How to add stoplight buttons to an OS X application programmatically - objective-c

I'm building an OS X cocoa application and I'm not using interface builder (for a variety of reasons). I've got the application to the point of loading a menu, title bar and main window, but I can't seem to figure out how to add the stoplight buttons to the title bar (programmatically).
My AppDelegate.m looks like this:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
MainViewController *mVC = [MainViewController new];
[mVC showMainViewController];
}
Then code in the MainViewController then creates the menu, window, and loads the application, as follows:
//
// MainViewController.m
// TestApp
//
#import "MainViewController.h"
#implementation MainViewController
#synthesize menubar;
#synthesize appMenu;
#synthesize appMenuItem;
#synthesize quitMenuItem;
#synthesize appName;
#synthesize quitTitle;
#synthesize window;
#synthesize homeViewController;
#synthesize resolutionHeight;
#synthesize resolutionWidth;
#synthesize width;
#synthesize height;
- (id)init
{
self = [super init];
if (self) {
appName = #"TestApp";
[self setupMenubar];
[self setupMainWindow];
}
return self;
}
- (void)setupMenubar
{
// Set up the main menu
menubar = [NSMenu new];
appMenu = [NSMenu new];
appMenuItem = [NSMenuItem new];
quitTitle = [NSString stringWithFormat:#"Quit %#", appName];
quitMenuItem = [[NSMenuItem alloc] initWithTitle:quitTitle
action:#selector(terminate:)
keyEquivalent:#"q"];
[menubar addItem:appMenuItem];
[appMenu addItem:quitMenuItem];
[appMenuItem setSubmenu:appMenu];
[NSApp setMainMenu:menubar];
}
- (void)setupMainWindow
{
// set the dimensions of the application
resolutionWidth = [[NSScreen mainScreen] frame].size.width;
resolutionHeight = [[NSScreen mainScreen] frame].size.height;
width = resolutionWidth * 0.75;
height = resolutionHeight * 0.75;
window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, width, height)
styleMask:NSTitledWindowMask
backing:NSBackingStoreBuffered defer:NO];
[window cascadeTopLeftFromPoint:NSMakePoint(10, 10)];
// add window buttons
closeButton = [NSWindow standardWindowButton:NSWindowCloseButton forStyleMask:NSTitledWindowMask];
// set metadata for the window
[window setTitle:appName];
[window makeKeyAndOrderFront:nil];
}
- (void)showMainViewController
{
// Set app settings
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
}
#end
I've looked around and I'm kind of at a loss for how to proceed.

You might try changing your NSWindow init method to the following:
window = [[NSWindow alloc]
initWithContentRect:NSMakeRect(0, 0, width, height)
styleMask:NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask | NSResizableWindowMask
backing:NSBackingStoreBuffered defer:NO];
I believe that ORing in the additional masks automatically adds the corresponding buttons to the title bar for you: NSClosableWindowMask adds the close button, NSMiniaturizableWindowMask adds the minimize (center) button, and NSResizableWindowMask adds the zoom (rightmost) button.

Related

Sandbox-friendly installation of NSRecentDocumentsMenu without Nib

I am working on a project that does not (currently) use any nib files at all.
I'm trying to figure out whether there is any way to add the NSRecentDocumentsMenu Open Recent menu that doesn't require use of a private API.
The below link is all I've managed to find thus far:
http://lapcatsoftware.com/blog/2007/07/10/working-without-a-nib-part-5-open-recent-menu/
Can anyone offer insights?
I'm trying to figure out whether there is any way to add the NSRecentDocumentsMenu Open Recent menu that doesn't require use of a private API.
The following link is for a document-based app in swift without using nibs:
https://www.dropbox.com/s/cfvsz11oe00lnp5/DocBased_swift.zip?dl=0
It uses a modification of menu code found in this reference:
https://talk.objc.io/episodes/S01E145-setting-up-a-document-based-app
The demo may be run in Xcode by creating a macOS app using swift and a XIB interface, although we will not use the XIB. Create a new file called main.swift and remove the existing AppDelegate class. Copy/paste the source code found above into the main.swift file (use import Cocoa instead of Foundation). When you hit the Run button you will notice that the menus are grayed out and there is no Open Recent menu item. To fix this be certain to add Cocoa NSDocument Class and Role to the info.plist (see image in folder above). The Cocoa NSDocument Class should be the nameOfYourApp.Document for swift or just plain Document for objc. When the Run button is hit after making these additions you should have a document based app with a functioning Open Recent menu item. Please post followup if there are problems.
Objc source code is here:
https://www.dropbox.com/s/dvs1u6lpk4l5xhq/DocBased_objc.zip?dl=0
Instructions are similar to those for swift, except a macOS app is created using objective-c. Copy/paste the source code into main.m after deleting what Xcode supplied. For simplicity, also remove AppDelegate and the Document class (if you checked 'Document based' in the setup), although you could use them if you so desire. The info.plist will also have to be amended with the two additions as noted above.
#import <Cocoa/Cocoa.h>
#interface Document : NSDocument {
NSWindow *window;
NSTextView *txtView;
}
- (void) buildWindow;
#end
#implementation Document
- (id) init {
if (self = [super init]) {
[self buildWindow];
}
return self;
}
+ (BOOL)autosavesInPlace {
return YES;
}
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
return [[txtView string] dataUsingEncoding:NSUTF8StringEncoding];
}
- (BOOL) readFromURL:(NSURL *) absoluteURL ofType:(NSString *)typeName error:(NSError **)outError {
NSString *str = [[NSString alloc] initWithContentsOfURL:absoluteURL encoding:NSUTF8StringEncoding error:outError];
[txtView setString:str];
return YES;
}
- (void) buildWindow {
#define _wndW 780
#define _wndH 700
window = [[NSWindow alloc] initWithContentRect: NSMakeRect( 0, 0, _wndW, _wndH )
styleMask: NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable
backing: NSBackingStoreBuffered defer: NO];
[window center];
[window setTitle: #"Test window"];
[window cascadeTopLeftFromPoint:NSMakePoint(600,0)];
[window makeKeyAndOrderFront: nil];
// **** Window Controller is Necessary **** //
NSWindowController *windowController = [[NSWindowController alloc]initWithWindow:window];
[self addWindowController: windowController];
// ****** NSTextView with Scroll ****** //
NSScrollView *scrlView = [[NSScrollView alloc] initWithFrame:NSMakeRect( 20, 50, _wndW - 40, _wndH - 70 )];
[[window contentView] addSubview:scrlView];
[scrlView setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ];
[scrlView setHasVerticalScroller: YES];
txtView = [[NSTextView alloc] initWithFrame:NSMakeRect( 0, 0, _wndW - 40, _wndH - 70 )];
[txtView setUsesFindBar:YES];
[txtView setEditable:YES];
[txtView setAllowsUndo: YES];
txtView.automaticQuoteSubstitutionEnabled = NO;
[scrlView setDocumentView: txtView];
// ***** Quit btn ***** //
NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 50, 5, 40, 40 )];
[quitBtn setBezelStyle:NSBezelStyleCircular ];
[quitBtn setTitle: #"Q" ];
[quitBtn setAutoresizingMask: NSViewMinXMargin];
[quitBtn setAction:#selector(terminate:)];
[[window contentView] addSubview: quitBtn];
}
#end
#interface AppDelegate : NSObject <NSApplicationDelegate>
- (void) buildMenu;
#end
#implementation AppDelegate
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender {
return NO;
}
- (void) buildMenu {
NSMenu *mainMenu = [NSMenu new];
[NSApp setMainMenu:mainMenu];
// **** App Menu **** //
NSMenuItem *appMenuItem = [NSMenuItem new];
[mainMenu addItem:appMenuItem];
NSMenu *appMenu = [NSMenu new] ;
[appMenuItem setSubmenu:appMenu];
NSString *appName = [[NSProcessInfo processInfo] processName];
NSString *aboutTitle = [#"About " stringByAppendingString:appName];
[appMenu addItemWithTitle: aboutTitle action:#selector(orderFrontStandardAboutPanel:) keyEquivalent:#""];
[appMenu addItem:[NSMenuItem separatorItem]];
[appMenu addItemWithTitle: #"Quit" action:#selector(terminate:) keyEquivalent:#"q"];
// ******** File Menu ********//
NSMenuItem *fileMenuItem = [NSMenuItem new];
[mainMenu addItem:fileMenuItem];
NSMenu *fileMenu = [[NSMenu alloc] initWithTitle:#"File"];
[fileMenuItem setSubmenu:fileMenu];
[fileMenu addItemWithTitle: #"New" action: #selector(newDocument:) keyEquivalent:#"n"];
[fileMenu addItemWithTitle: #"Open..." action: #selector(openDocument:) keyEquivalent:#"o"];
[fileMenu addItem: [NSMenuItem separatorItem]];
[fileMenu addItemWithTitle: #"Close" action: #selector(performClose:) keyEquivalent:#"w"];
[fileMenu addItemWithTitle: #"Save" action: #selector(saveDocument:) keyEquivalent:#"s"];
[fileMenu addItemWithTitle: #"SaveAs..." action: #selector(saveDocumentAs:) keyEquivalent:#""];
// ******** Edit Menu ********//
NSMenuItem *editMenuItem = [NSMenuItem new];
[mainMenu addItem:editMenuItem];
NSMenu *editMenu = [[NSMenu alloc] initWithTitle:#"Edit"] ;
[editMenuItem setSubmenu:editMenu];
[editMenu addItemWithTitle: #"Undo" action:#selector(undo:) keyEquivalent:#"z"];
[editMenu addItemWithTitle: #"Redo" action:#selector(redo:) keyEquivalent:#"Z"];
[editMenu addItem:[NSMenuItem separatorItem]];
[editMenu addItemWithTitle: #"Cut" action:#selector(cut:) keyEquivalent:#"x"];
[editMenu addItemWithTitle: #"Copy" action:#selector(copy:) keyEquivalent:#"c"];
[editMenu addItemWithTitle: #"Paste" action:#selector(paste:) keyEquivalent:#"v"];
[editMenu addItemWithTitle: #"Delete" action:#selector(delete:) keyEquivalent:#""];
[editMenu addItemWithTitle: #"Select All" action:#selector(selectAll:) keyEquivalent:#"a"];
[editMenu addItem:[NSMenuItem separatorItem]];
NSMenuItem *findItem = [editMenu addItemWithTitle:#"Find" action:#selector(performTextFinderAction:) keyEquivalent:#"f"];
[findItem setTag: NSTextFinderActionShowFindInterface];
}
- (void) applicationWillFinishLaunching: (NSNotification *)notification {
[self buildMenu];
}
- (void) applicationDidFinishLaunching: (NSNotification *)notification {
}
#end
int main() {
NSApplication *application = [NSApplication sharedApplication];
AppDelegate *appDelegate = [[AppDelegate alloc] init];
[application setDelegate:appDelegate];
[application run];
return 0;
}
Unable to post image of plist. Sorry.

Cocoa Idiomatic First Responder and Keyboard Event Handling Design

I've been reading the Cocoa Event Handling Guide to understand how to handle keyboard events for my application. However, I'm not quite sure if the solution I've found is the idiomatic way to approach this problem or even the best.
I have a view called CustomView and controller called CustomViewController that contains a NSTextView (within a NSScrollView) and a NSTextField. Basically a chat window.
What I would like is that even if the NSTextField is not selected, and the user starts typing, the NSTextField should be made the first responder and the text the user types should be appended to the NSTextField.
I've come up with a solution (below) where my RootWindowController captures keyDown events, makes the NSTextField the first responder, gets the field editor, and appends the text.
Is this the idiomatic approach Cocoa developers take or is their a simpler way that I am missing? Code is below. Thanks.
RootWindowController.m
#import "RootWindowController.h"
#implementation RootWindowController
- (id)initWithWindow:(NSWindow *)window
{
self = [super initWithWindow:window];
if (self) {
self.customViewController = [[CustomViewController alloc] init];
NSView *contentView = self.window.contentView;
CGFloat viewWidth = contentView.frame.size.width;
CGFloat viewHeight = contentView.frame.size.height;
self.customViewController.customView.textField.frame =
NSMakeRect(0, 0, viewWidth, 50);
self.customViewController.customView.scrollView.frame =
NSMakeRect(0, 51, viewWidth, viewHeight - 50);
self.customViewController.customView.textView.frame =
NSMakeRect(0, 51, viewWidth, viewHeight - 50);
self.window.contentView = self.customViewController.customView;
}
return self;
}
- (void)keyDown:(NSEvent *)theEvent
{
[self.window
makeFirstResponder:self.customViewController.customView.textField];
NSString *characters = theEvent.characters;
NSText *fieldEditor = [self.window fieldEditor:YES
forObject:self.customViewController.customView.textField];
[fieldEditor setString:
[NSString stringWithFormat:#"%#%#", fieldEditor.string, characters]];
[fieldEditor setSelectedRange:NSMakeRange(fieldEditor.string.length, 0)];
[fieldEditor setNeedsDisplay:YES];
}
#end
CustomViewController.m
#import "CustomViewController.h"
#implementation CustomViewController
- (id)init
{
self = [super init];
if (self) {
self.customView = [[CustomView alloc] initWithFrame:NSZeroRect];
[self.customView setAutoresizesSubviews:YES];
self.view = self.customView;
}
return self;
}
#end
CustomView.m
#import "CustomView.h"
#implementation CustomView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.scrollView = [[NSScrollView alloc] initWithFrame:NSZeroRect];
self.textView = [[NSTextView alloc] initWithFrame:NSZeroRect];
self.textField = [[NSTextField alloc] initWithFrame:NSZeroRect];
[self.textView setAutoresizesSubviews:YES];
[self.scrollView setDocumentView:self.textView];
[self.scrollView setHasVerticalScroller:YES];
[self.scrollView setAutoresizesSubviews:YES];
[self.textField setStringValue:#"Placeholder Text"];
[self addSubview:self.scrollView];
[self addSubview:self.textField];
[self.textView setNeedsDisplay:YES];
}
return self;
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
#end

NSWindow doesn't receive keyboard events

I creating NSWindow programmatically and i can't receive any keyboard messages. Instead of this i typing in Xcode editor, but my window is in focus at this time. How i can intercept this events?
Here is my code:
//// delegate
#interface MyDelegate : NSObject
#end
#implementation MyDelegate
#end
//// view
#interface MyView : NSView
#end
#implementation MyView
- (BOOL)isOpaque { return YES;}
- (BOOL)canBecomeKeyView { return YES;}
- (BOOL)acceptsFirstResponder { return YES;}
- (void)keyDown:(NSEvent *)event
{
printf("PRESS\n"); // it's ignoring
}
#end
//// main
int main(int argc, const char **argv){
[NSApplication sharedApplication];
NSWindow *window = [[NSWindow alloc]
initWithContentRect:NSMakeRect( 0, 0, 100, 100 )
styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[window setContentView: [[MyView alloc] init]];
[window setDelegate: [[MyDelegate alloc] init] ];
[window setAcceptsMouseMovedEvents:YES];
[window setLevel: NSFloatingWindowLevel];
[window makeKeyAndOrderFront: nil];
[NSApp run];
return 0;
}
You need to make the application a foreground application. This is required for the main window to receive key events.
ProcessSerialNumber psn = {0, kCurrentProcess};
OSStatus status =
TransformProcessType(&psn, kProcessTransformToForegroundApplication);

keyDown not being called

I have a custom NSView called SurfaceView. It is the contentView of a NSWindow and it handles basic events like mouse click and drawing. But don't matters what I do, it does not handle the keyDown function. I've already override the acceptsFirstResponder but nothing happens.
If it matters, I run the application using a custom NSEvent loop, shown below:
NSDictionary* info = [[NSBundle mainBundle] infoDictionary];
NSString* mainNibName = [info objectForKey:#"NSMainNibFile"];
NSApplication* app = [NSApplication sharedApplication];
NSNib* mainNib = [[NSNib alloc] initWithNibNamed:mainNibName bundle:[NSBundle mainBundle]];
[mainNib instantiateNibWithOwner:app topLevelObjects:nil];
[app finishLaunching];
while(true)
{
NSEvent* event = [app nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate date] inMode:NSDefaultRunLoopMode dequeue:YES];
[app sendEvent:event];
// Some code is execute here every frame to do some tasks...
usleep(5000);
}
Here's the SurfaceView code:
#interface SurfaceView : NSView
{
Panel* panel;
}
#property (nonatomic) Panel* panel;
- (void)drawRect:(NSRect)dirtyRect;
- (BOOL)isFlipped;
- (void)mouseDown:(NSEvent *)theEvent;
- (void)mouseDragged:(NSEvent *)theEvent;
- (void)mouseUp:(NSEvent *)theEvent;
- (void)keyDown:(NSEvent *)theEvent;
- (BOOL)acceptsFirstResponder;
- (BOOL)becomeFirstResponder;
#end
--
#implementation SurfaceView
#synthesize panel;
- (BOOL)acceptsFirstResponder
{
return YES;
};
- (void)keyDown:(NSEvent *)theEvent
{
// this function is never called
};
...
#end
Here's how I create the view:
NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(left, top, wide, tall) styleMask:NSBorderlessWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask backing:NSBackingStoreBuffered defer:NO];
...
[window makeKeyAndOrderFront:nil];
SurfaceView* mainView = [SurfaceView alloc];
[mainView initWithFrame:NSMakeRect(0, 0, wide, tall)];
mainView.panel = panel;
[window setContentView:mainView];
[window setInitialFirstResponder:mainView];
[window setNextResponder:mainView];
[window makeFirstResponder:mainView];
I found out what was preventing the keyDown event from being called. It was the NSBorderlessWindowMask mask, it prevents the window from become the key and main window. So I have created a subclass of NSWindow called BorderlessWindow:
#interface BorderlessWindow : NSWindow
{
}
#end
#implementation BorderlessWindow
- (BOOL)canBecomeKeyWindow
{
return YES;
}
- (BOOL)canBecomeMainWindow
{
return YES;
}
#end
In addition to answer: Check in your IB checkbox for NSWindow.
Title Bar should be checked. It the similar to NSBorderlessWindowMask
In my case I had to override the keyDown method on both NSView and NSWindow (created a customization of both). In the NSWindow in the keyDown override I had to call
- (void) keyDown:(NSEvent *) event {
if (self.firstResponder != self) {
[self.firstResponder keyDown:event];
}
}
to let the event reach the view. Of course I had also to call makeFirstResponder first on the NSWindow and pass the contentView to it :
[window makeFirstResponder: [window contentView] ];

Trying to understand how to use xib files

I have a custom view that I'm using in a xib file. I load the view and add it to a window. It adds the view just fine as I can see the default text of the labels in the view, but when I try to change the label with a method call, it doesn't change the text.
The custom view isn't anything to fancy, just draws a rounded, transparent background.
NotificationView.h
#import <Cocoa/Cocoa.h>
#interface NotificationView : NSView
#property (weak) IBOutlet NSTextField *primaryLabel;
#property (weak) IBOutlet NSTextField *secondaryLabel;
#property (weak) IBOutlet NSTextField *identifierLabel;
#end
NotificationView.m
#implementation NotificationView
#synthesize primaryLabel;
#synthesize secondaryLabel;
#synthesize identifierLabel;
- (id) initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self)
{
return self;
}
return nil;
}
- (void)drawRect:(NSRect)dirtyRect
{
NSColor *bgColor = [NSColor colorWithCalibratedWhite:0.0 alpha:0.6];
NSRect rect = NSMakeRect([self bounds].origin.x + 3, [self bounds].origin.y + 3, [self bounds].size.width - 6, [self bounds].size.height - 6);
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:5.0 yRadius:5.0];
[path addClip];
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowColor:[NSColor redColor]];
[shadow setShadowBlurRadius:2.0f];
[shadow setShadowOffset:NSMakeSize(0.f, -1.f)];
[shadow set];
[bgColor set];
NSRectFill(rect);
[super drawRect:dirtyRect];
}
#end
In the xib I have a custom view set to the type NotificationView. I've added 3 labels to the view and connected them to the above IBOutlets. (I ctrl-click & drag from the label to the .h file to make the connection.)
I'm loading the view and adding it to a window with the following method. It looks through an array of windows, if an existing match is found it used that window, if not it creates a new window.
- (void) popupNotificationWithTag:(NSString *)tag fade:(double)msFade lineOne:(NSString *)lineOneText lineTwo:(NSString *)lineTwoText
{
NotificationWindow *notificationWindow;
NotificationWindow *tmpWindow;
NSEnumerator *enumerator;
// Walk the notification windows in the array
enumerator = [self.notificationWindows objectEnumerator];
if(enumerator)
{
while((tmpWindow = [enumerator nextObject]))
{
if([tmpWindow.tag isEqualToString:tag])
{
notificationWindow = tmpWindow;
}
}
}
// Make a new notification window
if (!notificationWindow)
{
int width = [[NSScreen mainScreen] frame].size.width;
int height = [[NSScreen mainScreen] frame].size.height;
notificationWindow = [[NotificationWindow alloc] initWithRect:NSMakeRect(width - 420, height - 130, 400, 100)];
NSNib *nib = [[NSNib alloc] initWithNibNamed:#"Notification" bundle: nil];
NSArray *objects;
[nib instantiateNibWithOwner:self topLevelObjects:&objects];
for (id obj in objects) {
if ([[obj class] isSubclassOfClass:[NSView class]])
[notificationWindow setContentView:obj];
}
[notificationWindow setTag:tag];
[self.notificationWindows addObject:notificationWindow];
}
// Display window
[notificationWindow makeKeyAndOrderFront:nil];
[notificationWindow display];
notificationWindow.fadeOut = msFade;
[notificationWindow setPrimaryText:lineOneText];
[notificationWindow setSecondaryText:lineTwoText];
[notificationWindow setIdentifierText:tag];
}
The window class is NotificationWindow.h
#import <Foundation/Foundation.h>
#interface NotificationWindow : NSWindow
#property (nonatomic, strong) NSString *tag;
#property (nonatomic) double fadeOut;
- (id)initWithRect:(NSRect)contentRect;
- (void) setPrimaryText:(NSString *)text;
- (void) setSecondaryText:(NSString *)text;
- (void) setIdentifierText:(NSString *)text;
#end
NotificationWindow.m
#import "NotificationWindow.h"
#import "NotificationView.h"
//===========================================================================================================================
// Private call properties and methods
//===========================================================================================================================
#interface NotificationWindow()
#property (nonatomic,strong) NSTimer *timerFade;
- (void) timerFadeFired;
#end
//===========================================================================================================================
//===========================================================================================================================
#implementation NotificationWindow
//===========================================================================================================================
// Property Getters and Setters
//===========================================================================================================================
#synthesize tag = _tag;
#synthesize fadeOut = _fadeOut;
#synthesize timerFade = _timerFade;
//===========================================================================================================================
// Public methods
//===========================================================================================================================
- (id)initWithRect:(NSRect)contentRect
{
if (self = [super initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO]) {
[self setLevel: NSScreenSaverWindowLevel];
[self setBackgroundColor: [NSColor clearColor]];
[self setAlphaValue: 1.0];
[self setOpaque: NO];
[self setHasShadow: NO];
[self setIgnoresMouseEvents: YES];
[self setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
[self orderFront: NSApp];
self.fadeOut = -1;
// Start our timer to deal with fadeing the window
self.timerFade = [NSTimer scheduledTimerWithTimeInterval:0.001
target:self
selector:#selector(timerFadeFired)
userInfo:nil
repeats:YES];
return self;
}
return nil;
}
- (BOOL) canBecomeKeyWindow
{
return YES;
}
- (void) display
{
[super display];
[self setAlphaValue:1.0];
}
- (void) setPrimaryText:(NSString *)text
{
NotificationView *view = self.contentView;
view.primaryLabel.stringValue = text;
}
- (void) setSecondaryText:(NSString *)text
{
NotificationView *view = self.contentView;
view.secondaryLabel.stringValue = text;
}
- (void) setIdentifierText:(NSString *)text
{
NotificationView *view = self.contentView;
view.identifierLabel.stringValue = text;
}
//===========================================================================================================================
// Private methods
//===========================================================================================================================
- (void) timerFadeFired
{
[self orderFront:NSApp];
if (self.fadeOut > 0)
{
self.fadeOut--;
}
else if (self.fadeOut == 0)
{
if (self.alphaValue > 0)
self.alphaValue -= 0.002;
else
self.fadeOut = -1;
}
}
#end
So I assume I'm doing something wrong connecting the labels to the IBOutlets, but I can't figure out what. I suppose I could create the view in code, but I was trying to be good and use the interface builder.
I'm in XCode 4.2.1.