【发布时间】:2015-01-18 05:42:12
【问题描述】:
我有一个带有按钮的辅助窗口(面板)和一个包含一些标签的框。我需要能够更改框标题和标签文本,但只能在 AwakeFromNIB 中这样做。如果我在 WindowDidLoad 中检查标签的文本,它是零。 Init 正在触发 3 次。
// ExtendedCal.h
#import <Cocoa/Cocoa.h>
@interface ExtendedCal : NSWindowController
@property (retain) IBOutlet NSTextField *lblCCW;
@property (retain) IBOutlet NSTextField *lblCW;
@property (nonatomic, retain) IBOutlet NSTextField *lblDegrees;
@property (nonatomic, retain) IBOutlet NSBox *boxExtCal;
@property (nonatomic, retain) IBOutlet NSButton *btnOK;
@property (nonatomic, retain) NSString *maxSteps;
@property (nonatomic) NSInteger arrayCtr;
@property (nonatomic) NSInteger steps;
- (IBAction)btnOKClicked:(id)sender;
- (void)resetUI;
@end
// ExtendedCal.m
#import "ExtendedCal.h"
NSString * const myExtendedCalChangedNotification = @"MyExtCalKey";
NSString *const myExtendedCalEndedNotification = @"myExtCalEndedKey";
NSMutableArray *pointArray;
NSInteger extCalStep;
NSInteger extCalAxis;
NSInteger extCalMin;
NSInteger extCalMax;
/*
@interface ExtendedCal ()
@end
*/
@implementation ExtendedCal
@synthesize lblDegrees;
@synthesize lblCCW;
@synthesize lblCW;
@synthesize btnOK;
@synthesize boxExtCal;
@synthesize maxSteps;
@synthesize arrayCtr;
@synthesize steps;
- (id)init
{
self = [super initWithWindowNibName:@"ExtendedCal"];
return self;
}
- (void)windowWillLoad
{
[lblDegrees setStringValue:@"MMM"]; /// does nothing
}
- (void)windowDidLoad
{
[super windowDidLoad];
NSInteger var1, ec, tmpI = 0;
steps = 0;
[self resetUI]; //does nothing when called from here
maxSteps = @"16";
ec = 0;
pointArray = [NSMutableArray array];
var1 = 0;
while (var1 <= extCalStep*25)
{
if (extCalMin == var1)
{
ec = 1;
[pointArray addObject:[NSNumber numberWithInteger:var1]];
}
var1 = var1 + extCalStep;
}
if(ec==0)
{
ec = 1;
[pointArray addObject:[NSNumber numberWithInteger:extCalMin]];
}
var1 = extCalStep;
tmpI = [pointArray count];
while (var1 < extCalMax)
{
if (var1 > [[pointArray objectAtIndex:ec - 1]integerValue])
{
ec +=1;
[pointArray addObject:[NSNumber numberWithInteger:var1]];
}
var1 += extCalStep;
}
if(extCalMax > [[pointArray objectAtIndex:ec-1]integerValue])
{
ec +=1;
[pointArray addObject:[NSNumber numberWithInteger:extCalMax]];
}
tmpI = [pointArray count];
arrayCtr = 0;
NSString *str = [[pointArray objectAtIndex:0]stringValue];
[lblDegrees setStringValue:str]; //does nothing
NSString *s2 = [lblDegrees stringValue]; //s2 is nil
}
- (void)awakeFromNib
{
[lblDegrees setStringValue:@"888"]; //works from here
[self resetUI]; //works from here
}
- (void)btnOKClicked:(id)sender
{
extCalStep +=1;
NSMutableString *tmp;
NSString *option = @"160";
tmp = [NSMutableString string];
[tmp setString:@"Calibration-step "];
[tmp appendFormat:@"%ld",extCalStep];
[tmp appendString:@" of "];
[tmp appendString:maxSteps];
[boxExtCal setTitle:tmp]; //does nothing
option = [[pointArray objectAtIndex:arrayCtr] stringValue];
option = [NSString stringWithFormat:@"0%ld", extCalStep];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSDictionary *userInfo = [NSDictionary
dictionaryWithObject:optionforKey:@"myExtCalKey"];
[nc postNotificationName:myExtendedCalChangedNotification
object:self
userInfo:userInfo];
steps++;
}
- (void)resetUI
{
NSMutableString *boxStr = [[NSMutableString alloc] init];
[boxExtCal setTitle:@"Calibration step 1 of 16"];
lblCW.hidden = NO;
lblCCW.hidden = YES;
[boxStr setString:@""];
[boxStr appendString:@"999"];
//[boxStr appendString:@"\u00B0"];
[lblDegrees setStringValue:boxStr];
steps = 0;
}
接线
窗口被打开
// ORSSerialPortDemoController.m
#import "Calibrate.h"
#import "ExtendedCal.h"
Calibrate *calibrate;
ExtendedCal *extCal;
@interface ORSSerialPortDemoController () {
}
@end
.
.
.
[extendedcal showWindow:self];
【问题讨论】:
标签: objective-c cocoa