NSView subclass does not seem to start
NSView subclass does not seem to start
- Subject: NSView subclass does not seem to start
- From: Alex Hall <email@hidden>
- Date: Mon, 30 Dec 2013 17:34:54 -0500
Hello list,
A few days ago I said I am working on an audio game, when I asked about code organization. I have taken the advice I was given and am simply trying to get things to work, keeping the major portions separated as much as I can but mostly just wanting the project to do what I want. For the moment, what I want is to log keystrokes, so I can then implement a keystroke listener on the different views I will have. However, the single view I am trying to use logs nothing, but neither do I get any errors.
Before I continue I need to put out a reminder: I am *not* using Interface Builder, because there is no way for me, a Voiceover user, to reliably tie actions to UI elements. About a year ago I emailed this list about laying out UIs in code, and was provided with some extremely helpful code samples which I am trying now to use. The delay of almost a year, by the way, was partly my wanting to get more comfortable with Objective-C before taking on this type of project, and partly hoping that Apple would fix Xcode so I could fully use it. They have failed to do so, but I at least feel much more confident using Objective-c, so here I am once more.
Anyway, the problem remains that I need to capture keystrokes (and eventually mouse movements) in a subclass of NSView, but nothing seems to happen. Since this is an audio game, there is no need for any UI controls to be drawn - I need only the keyboard/mouse event handler. Therefore, my view has no UI controls, it is merely a rectangle, or should be, with an origin of (0,0). Below, and I apologize in advance for how long this will be, I have laid out the path taken to get the view on the screen. Where I feel it is necessary, I have given source code. Note that I still have the main XIB file Xcode generated. Perhaps I should remove it, I don't know yet.
1. My app runs, and the AppDelegate's applicationDidFinishLaunching method is called automatically. In here I create an AITGameController object,, which does the lion's share of the work. I then call this object's initGame and startLoop methods, the first to set up the game and the second to begin the game loop that will drive the app.
2. The AITGameController's init method sets up the AITViewController, whose source is below. It then sets up the AITAudioMenu (my NSView subclass, which is supposed to be logging keystrokes). Now we get into the fun part, so here's the code I am using:
//from AITGameController > init
[self setViewManager:[[AITViewController alloc] init]];
NSRect windowSize=NSMakeRect(0.0, 0.0, 200.0, 150.0);
NSWindow* mainWindow=[viewManager makeWindowWithRect:&windowSize style:NSTitledWindowMask|NSMiniaturizableWindowMask];
NSRect menuSize=NSMakeRect(0.0, 0.0, 100.0, 100.0);
AITAudioMenu* mainMenu=[[AITAudioMenu alloc] initWithFrame:menuSize];
[viewManager makeFrontWindow:mainWindow];
[viewManager addSubview:mainMenu toWindow:mainWindow];
I thought this would be enough, that adding the subview would cause things to start working, but it does not seem to. Now, here's the viewController code - whatever is wrong is probably in here somewhere.
//AITViewController.m
#import "AITViewController.h"
@implementation AITViewController
-(AITViewController*) init{
self=[super init];
if(!self) return nil;
return self;
}
-(NSWindow*) makeWindowWithRect:(NSRect) rect style:(NSUInteger) style{
NSWindow* theWindow= [[NSWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:YES];
return theWindow;
}
-(void) makeFrontWindow:(NSWindow*) theWindow{
[theWindow center];
[theWindow makeKeyAndOrderFront: self];
}
-(void) addSubview:(NSView*) theView toWindow:(NSWindow*) theWindow{
NSView* cv=[theWindow contentView];
[cv addSubview:theView];
}
@end
And just in case, here's the AudioMenu class:
//AITAudioMenu.m
#import "AITAudioMenu.h"
@implementation AITAudioMenu
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
// Drawing code here.
}
-(void) keyUp:(NSEvent*) event{
NSLog(@"Key pressed: %hu. Modifiers: %lu.", [event keyCode], [event modifierFlags]);
[super keyUp:event];
}
@end
You will notice I have done very little to the template Xcode gave me. Eventually I will do much more, but for now I just want that keyUp method to work. Once it is, I'll implement the actual functionality of the audio menu, but until I can detect keystrokes, I'd have nothing to which to bind the menu's methods anyway.
I hope all this makes some sense. I think what is tripping me up is trying to manage windows like this while IB is still being used, but I really don't know. Thanks for any help, and if anyone from Apple is on here: anytime you guys can make IB fully accessible, I and other budding developers would very much appreciate it. Yes, bug reports have been filed. :)
Have a great day,
Alex (msg sent from Mac Mini)
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden