0

首先,我对使用 Objective-C 进行编程非常陌生(大约 2.5 周),甚至对编写 OS X 可可应用程序的代码也很陌生。我试图在 AppDelegate.m 中设置 NSTextField 标签的值,其 IBOutlet 属性存在于另一个类中。我试图将它放在- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{}AppDelegate.m 的部分中,以便在 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

内存监控.h:

#import <Foundation/Foundation.h>
@interface MemoryMonitoring : NSObject

-(unsigned long long)getPhysicalMemoryValue;

@end

内存监控.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 使用点语法来访问属性,所以这一行似乎并没有像我预期的那样做。如果有人能阐明如何正确完成这项工作,我将不胜感激。

4

1 回答 1

0

There needs to be a UIViewController or a subclass of one involved. The textField must be part of a view hierarchy rooted with a view controller's view. Maybe start with a single view application template and add the text field to that view.

Then when that view controller sees viewWillAppear fire, it can ask the MemoryMonitoring class for the value and carry on setting it's own text field.

A good sign that you're on the right track is that you'll need to add virtually nothing to your app delegate code.

于 2014-06-25T22:43:58.887 回答