【发布时间】:2024-01-03 07:46:02
【问题描述】:
我要疯了!我只是不明白。 当我启动第二个窗口时,会在第二个窗口控制器中调用一个方法。该方法进行了大量计算,并应通过插座将一些结果放入标签中。标签保持为空。我不知道如何让它工作。
我的 AppDelegate.m:
#import "AppDelegate.h"
#import "ToDoItem.h"
#import "ResultWindowController.h"
@implementation AppDelegate
- (IBAction)pushRun:(id)sender {
if (rwc)
{
[rwc close];
}
rwc = [[ResultWindowController alloc] init];
[rwc calculateResults];//add observer
[rwc setShouldCascadeWindows:NO]; //window re-opens at the same position
[rwc showWindow:self];
}
@end
我的 ResultWindowController.h:
#import <Cocoa/Cocoa.h>
@interface ResultWindowController : NSWindowController
{
}
@property (weak) IBOutlet NSTextField *outputResultAverageValue;
@property (weak) IBOutlet NSTextField *outputResultToleranceValue;
-(void)calculateResults;
@end
ResultWindowController.m:
-(void)awakeFromNib
{
NSString *initial =@"-";
[_outputResultAverageValue setStringValue:initial];
[_outputResultToleranceValue setStringValue:initial];
}
- (void)calculateResults
{
double resultAverageValue = 0, resultToleranceValue = 0;
//calculations
for-loop{
resultAverageValue = (maxresult + minresult)/2;
resultToleranceValue = (maxresult - minresult)/2;
}
NSLog(@"resultaverage is:%f", resultAverageValue);
[_outputResultAverageValue setDoubleValue:resultAverageValue];
[_outputResultToleranceValue setDoubleValue:resultToleranceValue];
}
NSLog 给了我想要在标签中显示的值。我还可以使用awakeFromNib 方法初始化我的标签。
我有设计失败吗?我是否需要确保在 calculateResults 方法完成后设置标签?
提前致谢!!!
【问题讨论】:
-
_outputResultAverageValue的数据类型是什么?您尝试设置标签文本的代码在哪里? -
嗨 rmaddy,我添加了上面的代码。它是否需要始终是文本?
_outputResultAverageValue是一个连接到笔尖标签的 NSTextfield 出口。
标签: objective-c label outlet