Echilon
Echilon

Reputation: 10264

Overridden properties from superclass

I have a superclass of UIViewController - MasterViewController which declares a property called itemsViewController. This declares a method called from the MasterViewController, and is wired up via a storyboard in IB.

I have a subclass of MasterViewController which redeclares this property as a specific iPad version, but I can't access the redeclared property from the parent class.

MasterViewController

@interface MasterViewController : UIViewController {
}

@property (nonatomic, strong) IBOutlet ItemsViewController *itemsViewController;
@end

@implementation MasterViewController
@synthesize itemsViewController;

-(void)viewDidLoad {
// I can access itemsViewController in viewDidLoad.

}
@end 

MasterViewController_iPad

@interface MasterViewController_iPad : MasterViewController {
    IBOutlet ItemsViewController_iPad *_itemsViewController;
}

@property (nonatomic, strong) IBOutlet ItemsViewController_iPad *itemsViewController;
@end

@implementation MasterViewController_iPad
@synthesize itemsViewController = _itemsViewController;

-(void)viewDidLoad {
    [super viewDidLoad];
// when I call super viewDidLoad,  itemsViewController is nil, as though the property hasn't been overriden
// _itemsViewController is not nil in viewDidLoad.
}
@end 

Am I misunderstanding the way property inheritance works in Objective-C?

Upvotes: 1

Views: 1005

Answers (2)

Danil
Danil

Reputation: 1780

You can use category if you'd like to override property. Here is example:

I have PDFFileChooserViewController with PDFFileModel and PDFFilesDataSource and some logic related to this properties.

@class PDFFileModel, PDFFilesDataSource;
@interface PDFFileChooserViewController : UIViewController

@property (nonatomic, strong) PDFFileModel* selectedModel;
@property (nonatomic, strong) PDFFilesDataSource*dataSource;

@end

Then I'd like to add specific ViewController for choosing files from Dropbox but my model have some additional fields for example dropboxPath and my DropboxDataSource gets files using another way. So I decided to create category and override this properties:

#import "PDFFileChooserViewController.h"


@class DropboxFileModel,DropboxDataSource;

@interface DropboxViewController : PDFFileChooserViewController
@end

@interface DropboxViewController (ModelCategory)
@property(nonatomic, strong) DropboxFileModel* selectedModel;
@property(nonatomic, strong) DropboxDataSource* dataSource;
@end

Notice that this category will be visible inside DropboxViewController only where I can manipulate with that properties but another classes see only super class interface

Upvotes: 0

yuji
yuji

Reputation: 16725

You can't change the type signature of a method when you override a superclass method.

MasterViewController has these methods:

(void)setItemsViewController:(ItemsViewController *)foo
(ItemsViewController *)itemsViewController

But you're trying to give MasterViewController_iPad these methods:

(void)setItemsViewController:(ItemsViewController_iPad *)foo
(ItemsViewController_iPad *)itemsViewController

Which you can't do: you can't overload the same method name but have different types for the arguments.

If ItemsViewController_iPad is a subclass of ItemsViewController, a quick solution would be to keep the same signature as in MasterViewController but simply use an ItemsViewController_iPad when you set the property.

Upvotes: 1

Related Questions