Zack Newsham
Zack Newsham

Reputation: 2982

Accessing instance variables of a parent class, from its child

I have been learning Objective C for the iPhone for some weeks now, and today got my provisioning profile through, so wanated to put my app on my phone. However when I built it for my iPhone I got a list of errors (almost identical) of the type:

error: 'itemPickerView' undeclared (first use in this function)

When I built this for the simulator, this does not happen.

The errors all happen in a single class implementation file (ProjectSettingsController) which extends DetailController.

I have 2 other classes that also extend DetailController, and make use of the same instance variables as ProjectSettingsController does, but they do not cause errors.

ProjectSettingsController is slightly different, as the view it manages is never directly added to the navigation controller, however the compiler wouldnt know this at the time, and it works correctly on the simulator (both running IOS 4.3)

I guess my question is two fold:

  1. Why would something give me a compiler error when compiling for a device, and not when compiling for the simulator?

  2. Why is it giving me this error at all, only on this one class?

Here is the .h for DetailController

@interface DetailController :      UITableViewController<UIPickerViewDelegate,UIPickerViewDataSource>  {
    NSMutableDictionary * data;
    NSMutableArray * dataOrder;
    NSMutableDictionary * objectToData;
    UIDatePicker * pickerView;
    UIPickerView * itemPickerView;
    UIBarButtonItem * doneButton;
    UIBarButtonItem  *  savedRightButton;
    UIBarButtonItem  *  savedLeftButton;
    UIBarButtonItem * insertButton;
    UIView * slidingView;
    UITextView * textView;
    BOOL isEditing;
}

Here is the .h for ProjectSettingsController

#import <UIKit/UIKit.h>
#import "DetailController.h"
#import "Project.h"
#import "Task.h"
@class ProjectController;

@interface ProjectSettingsController : DetailController{
}
@property (nonatomic, retain) ProjectController* parent;
@property (nonatomic, retain) UINavigationController * navigationController;
@property (nonatomic, retain) UINavigationItem * navigationItem;
@property (nonatomic, retain) Project* project;

ProjectController is declared as a class, as opposed to included, as it already includes this class. I have tried adding DetailController as @class it did nothing, as I expected it would.

Here is a snippet of an offending piece of code from ProjectSettingsController.m

int hours = [itemPickerView selectedRowInComponent: 0] + 1;

Here is the .h for TaskController which works correctly

#import "DetailController.h"
#import "Task.h"
#import "Project.h"
#import "CompletionSlider.h"

@interface TaskController : DetailController{

}

And here is a snippet of code from TaskController.m that works.

[itemPickerView selectedRowInComponent:0]+1]

I have gone over the ins and outs of the two files for about 2 hours now, and can't find any reason why this would be happening.

Should I change the code to use properties, which would then meen going through my code and putting a super. in front of all of these variables? It seems silly to not be able to access instance variables from within a child class (especially since I seem to be able to access the methods?!)

I look forward to being told what silly thing it is that I have missed

Upvotes: 3

Views: 1089

Answers (1)

Walt Sellers
Walt Sellers

Reputation: 3939

Typically, when you change the build to target Simulator or Devices, you change the source of system header files you #import or #include. The differences in those header files may account for the change between simulator or device builds.

Whenever you suddenly get a list of compile errors, focus on the first error. Often a single error can cause further errors that cascade quickly into a huge list. Sometimes fixing the first error fixes all the rest.

Upvotes: 1

Related Questions