Вопрос

I am trying to make a TableViewController.. I got it to work using code from a youtube lesson: "Cocoa Programming L13-14" But then when I try to change it so that the default values aren't hard coded... but rather the values of controls in the Interface Builder, I get (null) across the board. Here is the code:

#import <Foundation/Foundation.h>

@interface Person : NSObject {
    IBOutlet NSPathControl* pcSource;
    IBOutlet NSPathControl* pcDestination;
    IBOutlet NSTextField* tfBackupAmount;

    NSURL* urlSource;
    NSURL* urlDestination;
    NSString* strBackupAmount;

    //Old--
    //NSString* name;
    //int age;
}

@property NSURL* urlSource;
@property NSURL* urlDestination;
@property NSString* strBackupAmount;

//Old--
//@property (copy) NSString* name;
//@property int age;

@end

and

#import "Person.h"

@implementation Person

@synthesize urlSource;
@synthesize urlDestination;
@synthesize strBackupAmount;

//Old--
//@synthesize name;
//@synthesize age;

- (id)init {
    self = [super init];
    if (self) {
        urlSource = [pcSource URL];
        urlDestination = [pcDestination URL];
        strBackupAmount = [tfBackupAmount stringValue];
        NSLog(@"%@\n%@\n%@",urlSource,urlDestination,strBackupAmount);

        //Old--
        //name = @"Yoda";
        //age = 900;
        //NSLog(@"%@: %i", name, age);
    }
    return self;
}
@end

Everything commented //Old-- worked, and interacted fine with the TableViewController. So I am assuming all that still works fine. The 3 controls (2 NSPathControl & 1 NSTextField) are linked up to an Object class:Person in Interface Builder with the controls linked up. Why am I getting output of:

(null)
(null)
(null)

? When I get to the NSLog(); line? Where am I going wrong? Thanks!

Это было полезно?

Решение 3

Ok, technically this question - I found an answer for it. It is to make a custom init method. In my case this means:

Person* p = [[Person alloc] initWithurlSource:[NSURL URLWithString:@"moo"] andurlDestination:[NSURL URLWithString:@"cow"] andstrBackupAmount:@"foo"];

However this still doesn't solve my problem of getting values for IBOutlets from another Class (in this case my TableViewController class) that has been exposed as @property:

@interface AppDelegate : NSObject <NSApplicationDelegate> {
.....
.....
@property (nonatomic, retain) NSPathControl* pcSource;
@property (nonatomic, retain) NSPathControl* pcDestination;
@property (nonatomic, retain) NSTextField* tfBackupAmount;

I am still having trouble getting values for these controls in my "addButtonPressed" method with:

//ad is AppDelegate - declared in interface as AppDelegate* ad;
NSPathControl* pcSource = [ad pcSource];
NSPathControl* pcDestination = [ad pcDestination];
NSTextField* tfBackupAmount = [ad tfBackupAmount];

Другие советы

pcSource, pcDestination, or tfBackupAmount aren't initialized when your init method is called, so they're all nil. Sending a message to nil is legal in Objective-C, and you'll just get nil back. That means urlSource, urlDestination, and strBackupAmount are all nil too, and that's why you ge the log output you're seeing.

You need to change the log message to sometime after those variables are initialized.

Try putting the code in -viewDidLoad rather than -init. It all has to do with the order of events (-init gets called before any of the IB stuff happens.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top