How to access IBOutlets declared in superclass?

11,546

Solution 1

I didn't realize it was even possible to connect to superclasses in interface builder until about an hour ago. Since this was the only question I could find regarding how to do this, I'll add my answer, even though this question is old. My answer is with regard to Xcode 4, not Xcode 3.

As far as I can tell, you can't connect to outlets in a superclass using the assistant editor, but you can do it by clicking on "File's Owner" in IB. That should show all the outlets in Utilities->Connections Inspector. You can then Ctrl+Click on the outlet in the inspector (click on the '+' sign), and drag it over to your view in IB.

Solution 2

The solution for the problem with the IBOutlet .. is to change the class type to the Base Class in the identity inspector

enter image description here

connect using Control + drag and drop and

enter image description here

change it back to the child class

enter image description here

This works for me

BTW: i used Xcode 6

Solution 3

IB should be able to see outlets from superclasses, I have done this a number of times with no issues. Are you sure you are importing the superclass correctly (using #import instead of @class)? IB needs some way to track back to the superclass.

Solution 4

Switching between the super and subclass in the identity inspector allows you to connect your outlets across the classes. The only issue I found is when you attempt to do this with a UITableViewCell and its subclass. I wanted to re-assign the default textLabel and detailTextLabel instances to labels I create in Interface Builder. The workaround is to create substitute labels and then override the getters to point to these instead.

Share:
11,546

Related videos on Youtube

Sam Ritchie
Author by

Sam Ritchie

I'm a machine learning researcher and software engineer with extensive experience working on large-scale streaming applications at companies like Google, Twitter and Stripe. I'm the co-author of Summingbird, Algebird, and the co-founder of PaddleGuru and Racehub.

Updated on May 12, 2022

Comments

  • Sam Ritchie
    Sam Ritchie about 2 years

    I'm currently refactoring a couple of view controllers that share a few IBOutlets and IBAction methods. I moved the outlet declarations and the IBAction method into a superclass, cutting these out of the subclasses.

    Now, when I open up Interface Builder, I find that I can't see the outlets or actions declared in the superclass. The connections still exist, as I'd wired them up before the refactoring, but they're grayed out. (It's important to note that the connections also WORK, as my action fires on a button press, and my outlets are modified properly.)

    The question is, how can I get interface builder to recognize outlets from a superclass? Is this possible, and, if not, what do you all recommend?

    (Just for fun, here's my superclass header file:)

    @interface TFMainViewController : UIViewController {
        UIImageView *logoImage, *thinkfunImage;
        UIButton *buyFullButton;        
    }
    
    @property (nonatomic, retain) IBOutlet UIImageView *logoImage, *thinkfunImage;
    @property (nonatomic, retain) IBOutlet UIButton *buyFullButton;
    
    -(IBAction) buyFullVersion;
    
    @end
    

    EDIT: in case anyone's wondering, I'm using Xcode and IB 3.2.5, with the iOS 4.2 SDK.

  • Sam Ritchie
    Sam Ritchie over 13 years
    Kendall, taking a look at the other answer, are you duplicating anything, or is the entire declaration in the superclass? I'm using #import, and do have access to the properties within the subclasses, so I know the header import's working.
  • Brian
    Brian over 13 years
    Actually, Interface Builder will list properties from superclasses, but they must be declared in the header file (of the superclass only), and that header must be imported by the subclass (as Kendall said).
  • pvinis
    pvinis almost 9 years
    i have the same problem. tried that, with NSCollectionViewItem, so i can connect the textField outlet, but still its not working..
  • Motti Shneor
    Motti Shneor about 8 years
    I just created a test project for this - and it doesn't work. As of Xcode 7.x - IB only shows IBOutlets of the actual class declared for an object, not those of its superclasses.
  • SinisterMJ
    SinisterMJ about 8 years
    I was just playing around with this in Xcode 7.3 in Swift and it still works fine. I was able to have an IBOutlet declared in the superclass, and either drag from it into IB, or drag from IB to the outlet to connect it. The Assistant editor does not show the superclass in the Automatic section but you can navigate there, and IB shows the outlet as coming from the subclass (which is technically accurate). @SamRitchie - don't duplicate anything, just have the outlet in the superclass, not in the subclass.
  • SinisterMJ
    SinisterMJ about 8 years
    Or just navigate with Cmd-Click to the superclass in the assistant editor, then you can drag from the class to IB or vice versa to connect outlets or create new ones.