Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 6, 2006, 11:34 PM   #1
DavidCar
macrumors 6502a
 
Join Date: Jan 2004
Why can't I subclass NSApplication?

I can't figure out how to post and catch "NSApplicationDefined" events. The problem is in just demonstrating that I can intercept any event. To do this I am trying to subclass NSApplication, but I can't make the project accept the subclass. The following appears in "Interface Builder: Frequently Asked Questions"

"Many widgets have a Custom Class pane in the Info window. You can use this pane to specify that, at runtime, an object should be instantiated as a specific subclass. For example, you could make a subclass of NSApplication called MyApplication. To ensure that an instance of MyApplication is used at runtime, click the File’s Owner of your main nib file. Bring up the Custom Class pane and select MyApplication."

My project was created as a "Cocoa Document Based Application" and the closest thing I can find to a "main nib file" is MainMenu.nib which does have a "File's Owner" but applying the above Custom Class pane procedure and then running the program with the following subclass produces no NSLog messages during key or menu events. Where am I going wrong?

#import "MyApplication.h"

@implementation MyApplication

- (void)sendEventNSEvent *)theEvent {
NSLog(@"This is getting sendEvent:");
[super sendEvent:theEvent];
}

@end


BTW, I was originally trying to call NSRunAlertPanel from multiple subthreads, guarded by NSLock, but had some unexpected problems when all my threads locked up and my alert panel only partially displayed. I'm trying to solve that by calling all NSRunAlertPanels from the main thread via an application defined event, passing the information by subclassing NSEvent, and delaying the subthread with an NSLock until the AlertPanel is OKd. I didn't expect to have problems just positioning a method to intercept events.
DavidCar is offline   0 Reply With Quote
Old Mar 7, 2006, 03:53 AM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
I've no idea what you have done wrong! You should be able to subclass NSApplication (I can).

First of all create a new class in your project called, say MyNSApplicaion.

The header should be something like:

@interface MyNSApplication : NSApplication

...

@end

Now in XCode select your app in the Targets section and bring up the Target Info pane. Choose Properties and change the Principal Class to MyNSApplication.

This is optional: it works without it!
Now open MainMenu.nib. Drag the header into the window titled MainMenu.nib (English). Switch back to Instances from Classes. Select File's Owner and open the Inspector. Change the Custom Class to MyNSApplication.
robbieduncan is offline   0 Reply With Quote
Old Mar 7, 2006, 10:08 AM   #3
DavidCar
Thread Starter
macrumors 6502a
 
Join Date: Jan 2004
Quote:
Originally Posted by robbieduncan
...
Now in XCode select your app in the Targets section and bring up the Target Info pane. Choose Properties and change the Principal Class to MyNSApplication.

This is optional: it works without it!
...
I left my original MyApplication file unchanged, but did what you said about Principal Class in Properties in the Targets Info pane. That solved the problem. Thanks, thanks, thanks.

Interface Builder is still a mystery to me, as I only use it when I have to. I couldn't seem to find a good reference on the internet that would give me the essential scrap of info you just gave me.

DC
DavidCar is offline   0 Reply With Quote
Old Apr 22, 2011, 02:48 PM   #4
kikoalbiol
macrumors newbie
 
Join Date: Apr 2011
Try implement:

+(id)sharedApplication
{
return [[MyApplication alloc] init];
}
kikoalbiol is offline   -1 Reply With Quote
Old Apr 22, 2012, 01:33 PM   #5
wolfiexiii
macrumors newbie
 
Join Date: Apr 2012
Quote:
Originally Posted by kikoalbiol View Post
Try implement:

+(id)sharedApplication
{
return [[MyApplication alloc] init];
}
The above does not function - tries to create multiple application instances. Instead the following works.

+(id)sharedApplication
{
return [super sharedApplication];
}
wolfiexiii is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Add elements to View from subclass Pades iPhone/iPad Programming 1 May 9, 2014 06:51 AM
Subclassing a subclass - question regarding IBOutlets and IBActions moonman239 iPhone/iPad Programming 1 Sep 27, 2013 08:15 PM
Resolved: Trying to subclass NSView Hans Kamp Mac Programming 9 Mar 25, 2013 04:43 PM
Trying to subclass UIView Hans Kamp iPhone/iPad Programming 3 Mar 25, 2013 12:15 AM
A single instance of a UIView subclass across the cells of UICollectionView naphatkrit iPhone/iPad Programming 3 Feb 1, 2013 02:17 PM

Forum Jump

All times are GMT -5. The time now is 08:47 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC