How to access IBOutlets declared in superclass?

前端 未结 10 1496
执笔经年
执笔经年 2020-12-30 03:24

I\'m currently refactoring a couple of view controllers that share a few IBOutlets and IBAction methods. I moved the outlet declarations and the

相关标签:
10条回答
  • 2020-12-30 04:00

    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.

    0 讨论(0)
  • 2020-12-30 04:03

    I'm pretty sure that IB only looks at the actual class you're using to find outlets, and not at superclasses. I think that the easiest solution would be to leave the instance variable declarations in the superclass, but duplicate the @property lines in each subclass.

    0 讨论(0)
  • 2020-12-30 04:07

    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.

    0 讨论(0)
  • 2020-12-30 04:14

    I ran into a similar problem with a superclass, but it was due to a bug in Xcode (8.2) where Interface Builder doesn't show outlets in the Connection Inspector if those outlets have been declared with a _Nullable type annotation for Swift compatibility.

    Using nullable inside @property's parentheses appears to work around the problem.

    This Xcode bug seems to affect outlets in any class (ie. not just superclasses).

    0 讨论(0)
提交回复
热议问题