-1

I have a class HelloWorldViewController that has the following

-(void) addDate method
    NSDate *myDate = [myDatePIcker dateValue];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"YYYYMMdd"];   
    NSLog(@"addDate date is:%@",[dateFormat stringFromDate:myDate]);

When I call the above method from and IBAction like this...

- (IBAction)myAddDate:(id)sender {
    [self addDate];
}

It works as intended (see NSLog below)

  2013-03-14 09:05:33.149 APPNAME[3531:303] AWAKENED in Hello 2013-03-14 16:05:33 +0000
  2013-03-14 09:05:33.150 APPNAME[3531:303] AWAKENED in Hello (null)
  2013-03-14 09:05:35.898 APPNAME[3531:303] addDate date is:20130314
  2013-03-14 09:05:35.899 APPNAME[3531:303] addDate completed with date 20130314

But, if I call that -(void) function from a method (datePickerAction) in another class (TableViewController), which is subclassed, see below...

@implementation TableViewController:HelloWorldViewController

-(IBAction)datePickerAction:(id)sender{
    [self addDate];
}

This is the output from NSLog

2013-03-14 09:08:01.719 APPNAME[3549:303] AWAKENED in Hello 2013-03-14 16:08:01 +0000
2013-03-14 09:08:01.720 APPNAME[3549:303] AWAKENED in Hello (null)
2013-03-14 09:08:03.321 APPNAME[3549:303] addDate date is:(null)
2013-03-14 09:08:03.322 APPNAME[3549:303] addDate completed with date (null)
2013-03-14 09:08:03.323 APPNAME[3549:303] addDate date is:(null)
2013-03-14 09:08:03.323 APPNAME[3549:303] addDate completed with date (null)

I'm green, go easy :-) Any suggestions appreciated!

UPDATE: I attempted to implement the solution below. Here's the updated & additional code. The app won't launch now...any suggestions.

@implementation HelloWorldViewController;

-(id) init{

HelloWorldViewController *myHelloWorldController = [[HelloWorldViewController alloc] init];
return self;
}



#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h> 
#import "HelloWorldViewController.h"
#import "Date.h"

>

@interface TableViewController : NSTableView <NSTableViewDataSource> {

IBOutlet NSTableView *tableview;
NSMutableArray *list;}




#import "TableViewController.h"
#import "HelloWorldViewController.h"

 @implementation TableViewController{
HelloWorldViewController *myHelloWorldViewController;}

 - (id) init{

self = [super init];
if (self) {
    list = [[NSMutableArray alloc] init];
}
return self;}



 - (IBAction)datePickerAction:(id)sender{
    [myHelloWorldViewController  addDate];


    //NSLog(@"Action Finished with date %@", myRename);}
ericdmann
  • 357
  • 5
  • 20

1 Answers1

0

There is not enough information in your question to figure out what the issue is so the following is a guess...

You might be getting confused over how subclassing works.

In your addDate method you reference a variable myDatePicker. As this is not declared in addDate it is either an instance variable of the class or, less likely, a global variable.

Looking at addDate its local variable myDate would be nil if myDatePicker is nil - a message to nil is allowed in Objective-C and returns nil.

You must have an instance of addDate's class, HelloWorldViewController, for your first test to succeed, and that instance must have a non-nil myDatePicker.

Now to your second class, TableViewController, which is a subclass of HelloWorldViewController. In its instance method datePickerAction you call addDate as an inherited method, yet in this case you find it does not work but produces null values.

That would suggest that your TableViewController instance's inherited myDatePicker instance variable is nil.

This is where the confusion over subclassing might come in. You have to totally distinct objects: the first is an instance of the HelloWorldViewController class; the second is an instance of the TableViewController class. That second object contains as part of itself the instance variables of its superclass, HelloWordlViewController, and those variables are distinct from any other variables in other instances of HelloWordlViewController, in particular those of your first object...

My guess is your first object has a non-nil myDatePicker and in your second it is nil.

This may suggest that rather than TableViewController being a subclass go HelloWorldViewController your design requires it to contain a reference to a separate HelloWorldViewController instance, something along the lines of:

@interface TableViewController : NSViewController or NSObject
   ...
@end

@implementation TableViewController
{
   HelloWorldViewController *myHelloWorldViewController; // set up in init etc.
}

- (IBAction)myAddDate:(id)sender
{
   [myHelloWorldViewController addDate];
}

...

@end

HTH, but remember this is answer is a guess as there is not enough information in your question to know whether this is your issue.

Comment Response

You do appear to be misunderstanding the basic object & inheritance model. You have now added to HelloWorldViewController:

-(id) init
{
   HelloWorldViewController *myHelloWorldController = [[HelloWorldViewController alloc] init];
   return self;
}

First you must initialise your superclass in an init method, this is often just the statement:

self = [super init];

without this your instance will not be properly constructed.

Second you appear to be trying to initialise an instance variable of another class, in that you are creating an instance of HelloWorldViewController within the init method of HelloWorldViewController and assigning it to a local variable which just happens to have the same name as an instance variable of TableViewController - those two variables have no connection whatsoever (as in all the people called "William" in the world are not the same person).

But you've an even bigger problem, your HelloWorldViewController's init method now calls your HelloWorldViewController's init method which calls... Yes that is infinite, which on your computer means until the memory fills up and your application dies a horrible death.

You probably need to go and study the class and instance concepts as your code suggests you are not understanding them properly. Any good text on (inheritance-based) object-oriented concepts should do; these concepts are fundamentally the same in most current object-oriented languages including Java and C#.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
  • I added code about where I attempted this, now I'm having issues with the app launching. What information can I show to help in a diagnosis? Thank you! – ericdmann Mar 14 '13 at 19:32
  • Doh, the infinite call I should have caught. I see your point about the "William" comparison. I'm going to have a modify the structure of the program to instead of using my HelloWorldViewController to receive the delegate action of myDatePicker, it will delegate to TableViewController where it doesn't have to use the intermediate class. Thank you for your help figuring this out, it's much appreciated. I'm also going to freshen up my inheritance knowledge. :-) – ericdmann Mar 14 '13 at 20:42