NSButton: show alternate image on hover - objective-c

I've a NSButton with both an Image and Alternate Image. I would like the alternate image to be shown on hover. To solve this, I've extended the NSButton to show the alternate image when hovering the view. Is there a better solution to this?
My solution:
#interface HoverButton()
#property (nonatomic, strong) NSTrackingArea *trackingArea;
#property (nonatomic, strong) NSImage *imageTmp;
#end
#implementation HoverButton
-(void)mouseEntered:(NSEvent *)theEvent {
[super mouseEntered:theEvent];
[self updateImages];
self.image = self.alternateImage;
}
-(void)mouseExited:(NSEvent *)theEvent {
[super mouseExited:theEvent];
self.image = self.imageTmp;
}
- (void)updateImages {
self.imageTmp = self.image;
}
-(void)updateTrackingAreas
{
if(self.trackingArea != nil) {
[self removeTrackingArea:self.trackingArea];
}
int opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways);
self.trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds]
options:opts
owner:self
userInfo:nil];
[self addTrackingArea:self.trackingArea];
}
#end

I would say this is better suited to an NSButtonCell subclass. you can do it in one method and it won't apply to all NSButtons (I doubt thats what you actually want). Just set your button cell type in IB to your custom subclass.
here is some code I just wrote and tested that works:
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
if (_alternateImageOrKeyEquivalentFont && _bcFlags2.mouseInside) {
// the draw bezel call is optional. maybe you don't want it
[self drawBezelWithFrame:cellFrame inView:controlView];
[self drawImage:_alternateImageOrKeyEquivalentFont
withFrame:cellFrame
inView:controlView];
} else {
[super drawInteriorWithFrame:cellFrame
inView:controlView];
}
}
you will need to set the showsBorderOnlyWhileMouseInside to YES probably in an init method for the cell.

CustomButton.h
#interface CustomButton : NSButton
#property (getter=isMouseInside) BOOL mouseInside;
#end
CustomButton.m
#implementation CustomButton
+ (Class)cellClass
{
return [CustomButtonCell class];
}
- (instancetype)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self commonCustomButtonInit];
}
return self;
}
- (void)commonCustomButtonInit
{
NSTrackingArea *trackingArea = nil;
trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds]
options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect
owner:self
userInfo:nil];
[self addTrackingArea:trackingArea];
}
- (void)mouseEntered:(NSEvent *)event
{
self.mouseInside = YES;
if ([self.cell isKindOfClass:[CustomButtonCell class]])
{
CustomButtonCell *cell = self.cell;
cell.mouseInside = YES;
}
}
-(void)mouseExited:(NSEvent *)event
{
self.mouseInside = NO;
if ([self.cell isKindOfClass:[CustomButtonCell class]])
{
CustomButtonCell *cell = self.cell;
cell.mouseInside = NO;
}
}
#end
CustomButtonCell.h
#interface CustomButtonCell : NSButtonCell
#property (getter=isMouseInside) BOOL mouseInside;
#end
CustomButtonCell.m
#implementation CustomButtonCell
#end
Also see this answer.

Related

OSX: Why my app needs click on it first to receive keydown event?

I currently developing a small dictionary app under OSX for my own use, I would like to have a feature that when I hit the return key, the focus would go to the nssearchfeild.
So I try to make the app to receive keyDown event using a NSView and NSViewController told by this tutorial.
But every time I start the app, it wouldn't receive the keyDown event. I have to click on the window once, then hit the keyboard, so that it can receive keyDown event.
What did I do wrong? Can anyone help me out with this problem? I have been stuck in this problem for days, and searching throught Google and API wouldn't help much.
Thanks in advance!
Here is my code for AppDelegate.m
#import "AppDelegate.h"
#import "MyDictViewController.h"
#interface AppDelegate()
#property (nonatomic,strong) IBOutlet MyDictViewController *viewController;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.viewController = [[MyDictViewController alloc] initWithNibName:#"MyDictViewController" bundle:nil];
[self.window.contentView addSubview:self.viewController.view];
self.viewController.view.frame = ((NSView*)self.window.contentView).bounds;
[self.window makeKeyAndOrderFront:nil];
}
#end
And My ViewController.m
#import "MyDictViewController.h"
#import "FileHelper.h"
#import <Carbon/Carbon.h>
#interface MyDictViewController ()
#property (weak) IBOutlet NSTableView *wordsFilteredTable;
#end
#implementation MyDictViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.IMAGE_FILE = [NSImage imageNamed:#"Document.png"];
self.wordlist = [FileHelper readLines];
self.filterWordlist = [[NSMutableArray alloc] init];
}
return self;
}
- (void)loadView
{
[super loadView];
[self.view becomeFirstResponder];
}
-(void)keyDown:(NSEvent*)theEvent
{
NSLog(#"Caught key event");
}
-(void)keyUp:(NSEvent *)theEvent
{
unsigned short keycode = [theEvent keyCode];
switch (keycode)
{
case kVK_Return:
[self.searchField becomeFirstResponder];
default:
break;
}
}
-(void)mouseDown:(NSEvent*)theEvent
{
NSLog(#"Caught mouse event");
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
self.wordsFilteredTable.rowHeight = 37;
NSTableCellView *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
if( [tableColumn.identifier isEqualToString:#"WordColumn"] )
{
NSString *word = [self.filterWordlist objectAtIndex:row];
cellView.textField.stringValue = word;
cellView.imageView.image = self.IMAGE_FILE;
return cellView;
}
return cellView;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [self.filterWordlist count];
}
- (void)controlTextDidChange:(NSNotification *)obj
{
NSTextView* textView = [[obj userInfo] objectForKey:#"NSFieldEditor"];
self.currentWord = [textView string];
[self.filterWordlist removeAllObjects];
for(NSString* word in self.wordlist) {
if ([word hasPrefix:self.currentWord]) {
[self.filterWordlist addObject:word];
}
}
[self.wordsFilteredTable reloadData];
}
#end
And my AppView.m
#import "AppView.h"
#implementation AppView
- (void)setViewController:(NSViewController *)newController
{
if (viewController)
{
[super setNextResponder:[viewController nextResponder]];
[viewController setNextResponder:nil];
}
viewController = newController;
if (newController)
{
[super setNextResponder: viewController];
[viewController setNextResponder:[self nextResponder]];
}
}
- (void)setNextResponder:(NSResponder *)newNextResponder
{
if (viewController)
{
[viewController setNextResponder:newNextResponder];
return;
}
[super setNextResponder:newNextResponder];
}
#end

How can i move to myScene from UIViewController?

I have 2 viewcontroller. In the first viewcontroller, there is the introduction and logo of the game and in the second viewcontroller it loads the skscene. And I travel from my first viewcontroller to second viewcontroller with a button click. But it becones error. My code is in the second viewcontroller is:
#interface MPAPViewSceneController()
#property (nonatomic, retain) MPAPMyScene *targetScene;
#property SKView *spriteView;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
_spriteView = (SKView *)self.view;
_spriteView.showsFPS = YES;
_spriteView.showsNodeCount = YES;
_spriteView.showsDrawCount = YES;
}
-(void)viewWillAppear:(BOOL)animated {
self.targetScene = [[MPAPMyScene alloc] initWithSize:CGSizeMake(768.0f, 1024.0f)];
[_spriteView presentScene:self.targetScene];
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:YES];
self.targetScene = nil;
}
And in the scene I have written the code given below:
#interface MPAPMyScene()
#property (nonatomic) BOOL contentCreated;
#end
#implementation MPAPMyScene
-(void)didMoveToView:(SKView *)view {
if (self.contentCreated == NO) {
[self createSceneContents];
self.contentCreated = YES;
}
}
-(void)willMoveFromView:(SKView *)view {
[self removeAllChildren];
}
-(void)createSceneContents {
self.backgroundColor = [UIColor blueColor];
self.scaleMode = SKSceneScaleModeAspectFit;
}
Try changing this
_spriteView = (SKView *)self.view;
to
_spriteView = (SKView *)self.targetScene;

Implement "Empty View" for NSTabView

I've been experimenting with JUEmptyView (a custom Cocoa control / NSView subclass, which displays a custom center-aligned placeholder in the middle of the view when all subviews have been removed).
So, I tried implementing the same thing for an NSTabView - simply by making it an NSTabView subclass (and re-setting my initial NSTabView item class).
Generally, it does work - it does show the placeholder (when the last tab is closed).
However, some issues still exist :
The background remains as that of a tabitem (thought they've all been closed)
When resizing the window (given that the NSTabView stretches all the way from left to right and from top to bottom), the view seems like it fails to redraw itself properly.
Example :
Full Code :
Interface
#import <Cocoa/Cocoa.h>
#interface JUTabEmptyView : NSTabView
{
#private
BOOL forceShow;
NSString *title;
NSColor *titleColor;
NSFont *titleFont;
NSColor *backgroundColor;
}
#property (nonatomic, copy) NSString *title;
#property (nonatomic, retain) NSFont *titleFont;
#property (nonatomic, retain) NSColor *titleColor;
#property (nonatomic, retain) NSColor *backgroundColor;
#property (nonatomic, assign) BOOL forceShow;
- (id)initWithTitle:(NSString *)title;
- (id)initWithTitle:(NSString *)title andFont:(NSFont *)font;
- (id)initWithTitle:(NSString *)title font:(NSFont *)font color:(NSColor *)color andBackgroundColor:(NSColor *)backgroundColor;
#end
Implementation
#import "JUTabEmptyView.h"
#implementation JUTabEmptyView
#synthesize title, titleFont, titleColor, backgroundColor;
#synthesize forceShow;
#pragma mark -
#pragma mark Setter
- (void)setTitle:(NSString *)ttitle
{
[title autorelease];
title = [ttitle copy];
[self setNeedsDisplay:YES];
}
- (void)setTitleFont:(NSFont *)ttitleFont
{
[titleFont autorelease];
titleFont = [ttitleFont retain];
[self setNeedsDisplay:YES];
}
- (void)setTitleColor:(NSColor *)ttitleColor
{
[titleColor autorelease];
titleColor = [ttitleColor retain];
[self setNeedsDisplay:YES];
}
- (void)setBackgroundColor:(NSColor *)tbackgroundColor
{
[backgroundColor autorelease];
backgroundColor = [tbackgroundColor retain];
[self setNeedsDisplay:YES];
}
- (void)setForceShow:(BOOL)tforceShow
{
forceShow = tforceShow;
[self setNeedsDisplay:YES];
}
#pragma mark -
#pragma Drawing
- (void)drawRect:(NSRect)dirtyRect
{
if(forceShow || [[self subviews] count] == 0)
{
NSRect rect = [self bounds];
NSSize size = [title sizeWithAttributes:[NSDictionary dictionaryWithObject:titleFont forKey:NSFontAttributeName]];
NSSize bezierSize = NSMakeSize(size.width + 40.0, size.height + 20.0);
NSRect drawRect;
// Background
drawRect = NSMakeRect(0.0, 0.0, bezierSize.width, bezierSize.height);
drawRect.origin.x = round((rect.size.width * 0.5) - (bezierSize.width * 0.5));
drawRect.origin.y = round((rect.size.height * 0.5) - (bezierSize.height * 0.5));
[backgroundColor setFill];
[[NSBezierPath bezierPathWithRoundedRect:drawRect xRadius:8.0 yRadius:8.0] fill];
// String
drawRect = NSMakeRect(0.0, 0.0, size.width, size.height);
drawRect.origin.x = round((rect.size.width * 0.5) - (size.width * 0.5));
drawRect.origin.y = round((rect.size.height * 0.5) - (size.height * 0.5));
[title drawInRect:drawRect withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleColor, NSForegroundColorAttributeName,
titleFont, NSFontAttributeName, nil]];
}
}
- (void)willRemoveSubview:(NSView *)subview
{
[super willRemoveSubview:subview];
[self setNeedsDisplay:YES];
}
- (void)didAddSubview:(NSView *)subview
{
[super didAddSubview:subview];
[self setNeedsDisplay:YES];
}
#pragma mark -
#pragma mark Constructor / Destructor
- (void)constructWithTitle:(NSString *)ttitle font:(NSFont *)font color:(NSColor *)color andBackgroundColor:(NSColor *)tbackgroundColor
{
title = ttitle ? [ttitle copy] : [[NSString alloc] initWithString:#"No active document"];
titleFont = font ? [font retain] : [[NSFont boldSystemFontOfSize:[NSFont smallSystemFontSize]] retain];
titleColor = color ? [color retain] : [[NSColor colorWithCalibratedRed:0.890 green:0.890 blue:0.890 alpha:1.0] retain];
backgroundColor = tbackgroundColor ? [tbackgroundColor retain] : [[NSColor colorWithCalibratedRed:0.588 green:0.588 blue:0.588 alpha:1.000] retain];
}
- (id)initWithCoder:(NSCoder *)decoder
{
if(self = [super initWithCoder:decoder])
{
[self constructWithTitle:nil font:nil color:nil andBackgroundColor:nil];
}
return self;
}
- (id)initWithFrame:(NSRect)frameRect
{
if(self = [super initWithFrame:frameRect])
{
[self constructWithTitle:nil font:nil color:nil andBackgroundColor:nil];
}
return self;
}
- (id)initWithTitle:(NSString *)ttitle
{
if((self = [super init]))
{
[self constructWithTitle:ttitle font:nil color:nil andBackgroundColor:nil];
}
return self;
}
- (id)initWithTitle:(NSString *)ttitle andFont:(NSFont *)font
{
if((self = [super init]))
{
[self constructWithTitle:ttitle font:font color:nil andBackgroundColor:nil];
}
return self;
}
- (id)initWithTitle:(NSString *)ttitle font:(NSFont *)font color:(NSColor *)color andBackgroundColor:(NSColor *)tbackgroundColor
{
if((self = [super init]))
{
[self constructWithTitle:ttitle font:font color:color andBackgroundColor:tbackgroundColor];
}
return self;
}
- (void)dealloc
{
[title release];
[titleFont release];
[titleColor release];
[backgroundColor release];
[super dealloc];
}
#end
OK, so that's how I've managed to fix it.
It turns out this "Empty View" implementation, apart from print a rounded box with a label in it, in the very middle of the parent view, failed to re-draw the main background. So, all it takes is to repaint it...
In drawRect: just add :
[[NSColor grayColor] set]; // or any other color you prefer
NSRectFill([self bounds]);

Preventing parent views UIGestureRecongnizer executing within a UITableVIew subview

Problem:
I have a UIView that has a UITableView as a subview. The UIView has a UITapGestureRecognizer configured.
My Problem is that taps on the table are consumed by the UIView's gesture recognizer. The result is the table view never gets to see taps.
How can I either make the recognizer fail when a point is within the table's frame OR make the table the default consumer of the tap.
I have tried (as the code examples show) a number of methods pointInside:withEvent, hitTest:withEvent but can't quite figure out how to do it.
Here is code representing the problem:
Controller:
#import <UIKit/UIKit.h>
#import "ABCDTableView.h"
#interface ABCDFirstView : UIView
#property (nonatomic,strong) ABCDTableView *tableView;
#end
#import "ABCDFirstView.h"
#implementation ABCDFirstView
#synthesize tableView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self awakeFromNib];
}
return self;
}
- (void)awakeFromNib
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(viewTouch:)];
[self addGestureRecognizer:tap];
}
- (void)viewTouch:(UIGestureRecognizer *)gesture {
NSLog(#"view touched");
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
if (CGRectContainsPoint(self.tableView.bounds, point)) {
NSLog(#"point in table point as well as view");
return NO;
}
else if (CGRectContainsPoint(self.bounds, point)) {
NSLog(#"point only in view");
return YES;
}
NSLog(#"point not in view");
return NO;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *view = [super hitTest:point withEvent:event];
NSLog(#"view hittest res: %#",view);
return view;
}
#end
TableView
#import <UIKit/UIKit.h>
#interface ABCDTableView : UITableView
<UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) NSArray *list;
#end
#import "ABCDTableView.h"
#implementation ABCDTableView
#synthesize list;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self awakeFromNib];
}
return self;
}
- (void)awakeFromNib
{
self.delegate = self;
self.dataSource = self;
// create table list
self.list = [[NSArray alloc]
initWithObjects: #"one",#"two",#"three",#"four",#"five",
#"six", #"seven", #"eight", #"nine", #"ten", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.list count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"cell";
UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [self.list objectAtIndex:[indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"row selected");
}
#end
View
#import <UIKit/UIKit.h>
#import "ABCDTableView.h"
#interface ABCDFirstView : UIView
#property (nonatomic,strong) ABCDTableView *tableView;
#end
#import "ABCDFirstView.h"
#implementation ABCDFirstView
#synthesize tableView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self awakeFromNib];
}
return self;
}
- (void)awakeFromNib
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(viewTouch:)];
[self addGestureRecognizer:tap];
}
- (void)viewTouch:(UIGestureRecognizer *)gesture {
NSLog(#"view touched");
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
if (CGRectContainsPoint(self.tableView.bounds, point)) {
NSLog(#"point in table point as well as view");
return NO;
}
else if (CGRectContainsPoint(self.bounds, point)) {
NSLog(#"point only in view");
return YES;
}
NSLog(#"point not in view");
return NO;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *view = [super hitTest:point withEvent:event];
NSLog(#"view hittest res: %#",view);
return view;
}
#end
Use the UIGestureRecognizerDelegate method
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
and check if the touch.view is the same view as the one that should receive the gesture recognizer.

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.