问题
Available nested classes SuperView and NestedView.
class SuperView : UIImageView {
class NestedView : UIImageView {
var text : String = "Nested View"
}
var text : String = "Super View"
var nested : NestedView?
}
I would like to set for a UIImageView the property named "Custom Class Name" to value "NestedView" inside the inspector of the storyboard scene. But the Interface Builder couldn't find "NestedView" class.

回答1:
At this time, I think that Interface Builder only recognizes the names of Objective-C classes. You can still make Interface Builder find a nested class with the @objc keyword:
class SuperView: UIView {
@objc(SVNestedView) class NestedView: UIImageView {
}
}
Then, in Interface Builder, specify that th view is of class SVNestedView. Since Objective-C isn't namespaced, you still need to pick unique names for each nested class, but at least the Swift side is properly namespaced.
回答2:
In Swift, an instance of an inner class is independent of any instance of the outer class. It is as if all inner classes in Swift are declared using Java's static.
I don't think this class design suits your requirement. What you need to do is, you need to create a new class outside your view, and create some sort of a composition. This will surely work out for you.
Here is some modification to your code:
class SuperView : UIImageView {
var text : String = "Super View"
var nested : NestedView = NestedView()
}
class NestedView : UIImageView {
var text : String = "Nested View"
}
回答3:
I think it impossible to specify an inner class in a storyboard like SuperView.NestedView
so far. So I makes a class extended from an inner class, then specifies it in a storyboard like SuperView_NestedView
. It works for me.
final class SuperView_NestedView: SuperView.NestedView {} // Specifies this class in the storyboard
class SuperView : UIImageView {
class NestedView : UIImageView {
var text : String = "Nested View"
}
var text : String = "Super View"
var nested : NestedView?
}
This view can be referred as SuperView.NestedView
from a ViewController because SuperView_NestedView
is extended from SuperView.NestedView
.
class TheViewController: UIViewController {
@IBOutlet var nestedView: SuperView.NestedView!
来源:https://stackoverflow.com/questions/29066073/how-to-specify-nested-custom-view-class