How to access IBOutlets declared in superclass?
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
connect using Control + drag and drop and
change it back to the child class
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.
Related videos on Youtube
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, 2022Comments
-
Sam Ritchie about 2 years
I'm currently refactoring a couple of view controllers that share a few
IBOutlet
s andIBAction
methods. I moved the outlet declarations and theIBAction
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 over 13 yearsKendall, 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 over 13 yearsActually, 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 almost 9 yearsi have the same problem. tried that, with NSCollectionViewItem, so i can connect the textField outlet, but still its not working..
-
Motti Shneor about 8 yearsI 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 about 8 yearsI 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 about 8 yearsOr 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.