Apollo
Apollo

Reputation: 1986

Using IBOutlet in child class (Cocoa OSX)

Ok, so I have a class named "AmazingClass" that I use to generate a sheet to save a file. In it there is an NSView with extra options. It works well, but now I want to have more windows with the same ability, but using different options because they are supposed to save different file formats.

The basic layout is like this:

AmazingClass.h

#import <Cocoa/Cocoa.h>

@interface AmazingClass : NSObject {
    NSView * _accessoryView;
    BOOL     _prepared;
}
@property (assign, nonatomic) IBOutlet NSView * accessoryView;
@property (copy, nonatomic) NSString * nibForAcessoryView;
// Methods that generate the save window
- (void) prepareToRun;
@end

AmazingClass.m

#import "AmazingClass.h"

@implementation AmazingClass
@synthesize accessoryView = _accessoryView;
@synthesize nibForAcessoryView;
// Other stuff here
- (void) prepareToRun {
    // stuff here
    if ([self nibForAcessoryView] == nil) {
        [self setNibForAcessoryView: @"AmazingWindow"];
    }

    [NSBundle loadNibNamed:[self nibForAcessoryView] owner:self];

    _prepared = YES;
}

now I want to use the same class to handle different NSViews, like this:

NotSoAmazing.h

#import "AmazingClass.h"

@interface NotSoAmazing : AmazingClass {
    IBOutlet NSView * subAccessoryView;
}
@end

NotSoAmazing.m

#import "NotSoAmazing.h"

@implementation NotSoAmazing

- (void) prepareToRun {
    [self setAccessoryView:subAccessoryView];
    [self setNibForAcessoryView: @"NotSoAmazingWindow"];
    [super prepareToRun];
}

Then I create a new NSView, which File's owner set to the class "NotSoAmazing", I create all links in the interface and execute the code.

If there is an interface linking to the original class, I have the original interface used instead of the alternative one. If I remove all links, nothing is displayed.

Hence, my question: how can I handle IBoutlets in children classes to adopt a different view?

Upvotes: 0

Views: 110

Answers (1)

Caleb
Caleb

Reputation: 125037

There's no problem with subclasses having outlets just as their parent classes do. In this case, AmazingView has one outlet, accessoryView, and NotSoAmazing has two: accessoryView and subAccessoryView.

It's not clear to me whether you want NotSoAmazing to load its accessoryView from the same nib that AmazingView does, or if you want to connect that to a different view in NotSoAmazing's nib. I'd guess the latter given that you're calling -setNibForAccessoryView: in NotSoAmazing and setting it to a different nib file. Also, I don't see the logic in calling [self setAccessoryView:subAccessoryView] if you're going to load the subAccessoryView from a nib (or, if you're not, why is that an outlet?).

It looks like you're on the right track, but you need to decide exactly what you want to happen. Do you want to load both views from the same nib, or from two different nibs?

Upvotes: 1

Related Questions