I have a pretty simple app (at the moment) in development, and I'm having a
problem getting an NSTextField to update. (see code below)
Basically I have a custom view (NSView called buildView) and a text field
(called statusField0) that is contained in a "statusController" in my XIB.
I have connected everything in IB (that I know of). Now, the status
controller that I am intending to use will print out status messages to a
group of NSTextFields at the bottom of the window. statusController has
all the IBOutlets to these fields. buildView is intended to control (or
"intercept" if you will) Mouse and Key clicks in the custom buildView NSView,
and draw things in the window...but it will also need to update some status
info in the NSTextFields controlled by statusController.
The problem is that, on a keyboard click, the NSView correctly updates
itself (it redraws - right now it just removes some images from itself) but
I can't figure out a mechanism to update the NSTextField in statusController.
The sequence of events, taken from NSLog Console messages, is:
Now I have tried everything I can think of...a static String
in buildViewKeyDown, copying the string...bindings. Nothing
seems to allow me to update the NSTextField when the event
is called from "over the wall". How do I cause it to update.
No amount of setNeedsDisplay seems to work, either. And I've
also tried resignFirstResponder tactics.
I also have NSObjects of buildView and statusController class
in the XIB...are there direct connections there that I need.
Note that all the connections to the IBOutlets, pushbuttons etc.
are there and seem to work when the event comes from it's own
controller.
Please help, this is driving me crazy. I'm porting a card game
from Windows 32 API world and it's a very different mindset
over here already. I promise you'll enjoy the game if you help
me get it working. I have left the relevant portions of the
code in the listing below. Please don't hesitate with requests
for addtional information. Thanks in advance!
problem getting an NSTextField to update. (see code below)
Basically I have a custom view (NSView called buildView) and a text field
(called statusField0) that is contained in a "statusController" in my XIB.
I have connected everything in IB (that I know of). Now, the status
controller that I am intending to use will print out status messages to a
group of NSTextFields at the bottom of the window. statusController has
all the IBOutlets to these fields. buildView is intended to control (or
"intercept" if you will) Mouse and Key clicks in the custom buildView NSView,
and draw things in the window...but it will also need to update some status
info in the NSTextFields controlled by statusController.
The problem is that, on a keyboard click, the NSView correctly updates
itself (it redraws - right now it just removes some images from itself) but
I can't figure out a mechanism to update the NSTextField in statusController.
The sequence of events, taken from NSLog Console messages, is:
HTML:
2009-03-14 16:07:56.568 myApp[1083:10b] SC init
2009-03-14 16:07:56.569 myApp[1083:10b] SC init done
2009-03-14 16:07:56.570 myApp[1083:10b] SC init
2009-03-14 16:07:56.571 myApp[1083:10b] SC init done
2009-03-14 16:07:56.575 myApp[1083:10b] SC init
2009-03-14 16:07:56.576 myApp[1083:10b] SC init done
2009-03-14 16:07:56.578 myApp[1083:10b] SC AwakeFromNIB <- statusField0 has "Initial String"
2009-03-14 16:07:56.603 myApp[1083:10b] Accept bV
2009-03-14 16:07:56.603 myApp[1083:10b] Accept bV <- buildView accepts first responder
2009-03-14 16:07:56.620 myApp[1083:10b] buildView drawRect
2009-03-14 16:07:57.576 myApp[1083:10b] buildView drawRect <- draws splash graphics/logos fine
2009-03-14 16:08:00.485 myApp[1083:10b] Key 0x05 (5) Pressed <- keyDown in buildview calls [statCont buildViewKeyDown:@"FromBuildView"];
2009-03-14 16:08:00.485 myApp[1083:10b] buildViewKeyDown In <- NSLog proves it's being called
2009-03-14 16:08:00.485 myApp[1083:10b] buildViewKeyDown Out <- statusField0 is NOT updated, still "Initial String" ??????????
2009-03-14 16:08:00.486 myApp[1083:10b] buildView drawRect <- splash images disappear correctly
2009-03-14 16:08:03.209 myApp[1083:10b] Pushed <- statusController button pushed [self buildViewKeyDown:@"Pushed"];
2009-03-14 16:08:03.210 myApp[1083:10b] buildViewKeyDown In <- NS Log proves it's being called
2009-03-14 16:08:03.210 myApp[1083:10b] buildViewKeyDown Out <- statusField0 IS updated to "Pushed"
Now I have tried everything I can think of...a static String
in buildViewKeyDown, copying the string...bindings. Nothing
seems to allow me to update the NSTextField when the event
is called from "over the wall". How do I cause it to update.
No amount of setNeedsDisplay seems to work, either. And I've
also tried resignFirstResponder tactics.
I also have NSObjects of buildView and statusController class
in the XIB...are there direct connections there that I need.
Note that all the connections to the IBOutlets, pushbuttons etc.
are there and seem to work when the event comes from it's own
controller.
Please help, this is driving me crazy. I'm porting a card game
from Windows 32 API world and it's a very different mindset
over here already. I promise you'll enjoy the game if you help
me get it working. I have left the relevant portions of the
code in the listing below. Please don't hesitate with requests
for addtional information. Thanks in advance!
Code:
==========================================
statusController.h:
==========================================
#import <Cocoa/Cocoa.h>
@interface statusController : NSObject {
IBOutlet NSTextField *statusField0;
NSString *strStat0;
}
- (void) buildViewKeyDown:(NSString *)bstr;
- (IBAction)pushed:(id)sender;
@end
==========================================
statusController.m:
==========================================
#import "statusController.h"
@implementation statusController
//========================================
// awaken
//========================================
- (void)awakeFromNib {
NSLog(@"SC AwakeFromNIB");
NSFont* font = [NSFont fontWithName:@"Arial" size:11.0];
[statusField0 setFont:font];
[statusField0 setStringValue:strStat0];
}
//========================================
// called from statusContoller.pushed - works
// called from buildView.keyDown - DOESN'T work
//========================================
- (void) buildViewKeyDown:(NSString *)bstr {
NSLog(@"buildViewKeyDown In");
[statusField0 setStringValue:bstr];
[statusField0 setNeedsDisplay:YES];
NSLog(@"buildViewKeyDown Out");
}
//========================================
// button pushed from SC
//========================================
- (IBAction)pushed:(id)sender {
NSLog(@"Pushed");
[self buildViewKeyDown:@"Pushed"];
}
//========================================
// SC init
//========================================
- (id) init
{
[super init];
NSLog(@"SC init");
strStat0 = [[NSString alloc] initWithString:@"Initial String"];
NSLog(@"SC init done");
return self;
}
@end
==========================================
buildView.h:
==========================================
#import <Cocoa/Cocoa.h>
@class statusController;
@interface buildView : NSView {
NSImage *logoImage;
NSImage *copyRightImage;
NSTimer *copyTimer;
int showCopyRight;
int showLogo;
statusController *statCont;
}
- (void)startCopyTimer;
- (void)handleCopyTimer:(NSTimer *)aTimer;
@end
#import "buildView.h"
#import "statusController.h"
==========================================
buildView.m
==========================================
@implementation buildView
-(BOOL) acceptsFirstResponder {
NSLog(@"Accept bV");
return YES;
}
//========================================
// handleKeyboard
//========================================
-(void)keyDown:(NSEvent *)anEvent {
unsigned short kc = [anEvent keyCode];
NSLog(@"Key 0x%02x (%d) Pressed", kc, kc);
showCopyRight = 0;
showLogo = 0;
[statCont buildViewKeyDown:@"FromBuildView"];
[self setNeedsDisplay:YES];
}
//========================================
// initWithFrame - load all the images we need initially
//========================================
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// load some images I need later
}
if (!statCont) {
statCont = [[statusController alloc] init];
}
return self;
}
//========================================
// drawRect
//========================================
- (void)drawRect:(NSRect)rect {
NSLog(@"buildView drawRect");
NSRect bounds = [self bounds];
//=====================================
// set the background to Green
//=====================================
[[NSColor colorWithDeviceRed:0.282 green:0.498 blue:0.118 alpha:1.0] set];
[NSBezierPath fillRect:bounds];
...
}
@end