How to specify nested custom view class?
Asked Answered
K

3

19

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.

enter image description here

Komara answered 15/3, 2015 at 20:55 Comment(1)
My expectation, based on how nested types work elsewhere, would be that you could assign the class to SuperView.NestedView but interface builder does not allow this. I think this would be a great feature for namespacing small, purpose-built subviews.Counterword
A
17

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.

Argive answered 3/3, 2016 at 19:12 Comment(2)
This doesn't seem to work with Swift 3 in Xcode 8.1. Still can't see SVNestedView in IBStets
Unfortunately, it seems Xcode doesn't support showing connected IBOutlets for nested classes.Aspergillum
A
0

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"
}
Alsup answered 15/3, 2015 at 21:9 Comment(0)
P
0

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!

Pluperfect answered 30/8, 2019 at 3:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.