使用Objective-C设置NSTextField的标签stringValue,属性IBOutlet NSTextFi



首先,我是Objective-C编程的新手(大约2.5周),甚至是为OSX可可应用程序编写代码的新手。我正试图在AppDelegate.m中设置一个NSTextField标签的值,该标签的IBOutlet属性存在于另一个类中。我试图将其放在AppDelegate.m的- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{}部分中,以便在加载MainMenu.xib文件并在屏幕上显示之前设置NSTextField的值。到目前为止,我有以下代码:

AppDelegate.m:

#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{    
//  Get Physical memory in MB
    MemoryMonitoring *physicalMemoryObj = [[MemoryMonitoring alloc]init];
    unsigned long long physicalMemoryValue = [physicalMemoryObj getPhysicalMemoryValue];
//  Set the labels on the slider
    RamdiskSize *sizeLabels = [[RamdiskSize alloc]init];
    NSString *maxValue = [[NSString alloc] initWithFormat:@"%lluGB",(physicalMemoryValue / 1024)];
//  This line is not doing what I had expected
[sizeLabels.textLabelSizeMax setStringValue:maxValue];
}
@end

MemoryMonitoring.h:

#import <Foundation/Foundation.h>
@interface MemoryMonitoring : NSObject
-(unsigned long long)getPhysicalMemoryValue;
@end

MemoryMonitoring.m:

#import "MemoryMonitoring.h"
@implementation MemoryMonitoring
-(unsigned long long)getPhysicalMemoryValue{
    NSProcessInfo *pinfo = [NSProcessInfo processInfo];
    return ([pinfo physicalMemory] /1024/1024);
}
@end

RamdiskSize.h:

#import <Foundation/Foundation.h>
@interface RamdiskSize : NSObject
@property (weak) IBOutlet NSTextField *textLabelSizeMax;
@end

RamdiskSize.m:

#import "RamdiskSize.h"
#import "MemoryMonitoring.h"
@implementation RamdiskSize
@synthesize textLabelSizeMax;
@end

正如我的AppDelegate.m中所评论的,有问题的行是[sizeLabels.textLabelSizeMax setStringValue:maxValue];。我唯一的其他编程经验来自VBScript,据我所知,Objective-C使用点语法来访问属性,所以这一行似乎没有达到我预期的效果。如果有人能说明如何正确地完成这一操作,我将非常感谢您的输入。

需要一个UIViewController或一个的子类。textField必须是以视图控制器的视图为根的视图层次结构的一部分。也许可以从单个视图应用程序模板开始,并将文本字段添加到该视图中。

然后,当视图控制器看到viewWillAppear被激发时,它可以向MemoryMonitoring类请求该值,并继续设置自己的文本字段。

一个好迹象表明你走上了正确的轨道,那就是你几乎不需要在应用程序代理代码中添加任何内容。

最新更新