0

To start off with I am very new, (about 2.5 weeks) to programming in Objective-C and even newer to writing code for OS X cocoa apps. I am attempting to set the value of a NSTextField label in AppDelegate.m whose IBOutlet property exists in another class. I'm attempting to place this in the - (void)applicationDidFinishLaunching:(NSNotification *)aNotification{} section of AppDelegate.m so that the value of the NSTextField is set before the MainMenu.xib file is loaded and displayed on screen. Here is the following code that I have so far:

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

As commented in my AppDelegate.m, the line in question is [sizeLabels.textLabelSizeMax setStringValue:maxValue];. My only other programming experience is from VBScript and as far as I can tell Objective-C uses dot syntaxing to access properties, so this line doesn't seem to be doing what I had expected it to do. If anyone could shed some light on how this is to be done properly, I would greatly appreciate the input.

tshepang
  • 12,111
  • 21
  • 91
  • 136

1 Answers1

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.

danh
  • 62,181
  • 10
  • 95
  • 136