# Swift - adding controls to a TabPanel

**URL:** <https://ifnotnil.com/t/swift-adding-controls-to-a-tabpanel/2344>\
**Category:** Other\
**Created:** [15 February 2022 22:55 UTC](https://ifnotnil.com/t/swift-adding-controls-to-a-tabpanel/2344 "2022-02-15T22:55:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [15 February 2022 22:55 UTC](https://ifnotnil.com/t/swift-adding-controls-to-a-tabpanel/2344/1 "2022-02-15T22:55:49Z")

</div>

I have dozens of subclassed controls

```swift
class C1 : NSView {
vareventDelegate : myDelegate
... other stuff ...
}

class S1 : NSSlider {
var eventDelegate : myDelegate
... other stuff ...
}

```

Adding these to the base window works fine using a method like this

```swift
public func addToView(_ parent:NSView?) {
  parent!.addSubview(self)
  self.eventDelegate = parent!.window as? dsCONTROLDELEGATE
}

```

HOWEVER adding them to a TabPanel/Pagepanel is more difficult  
adding the control itself is easy

```swift
class myTab : NSTabView {
public func addToPanel(panel:Int,control:NSView) {
   self.tabViewItem(at: panel).view?.addSubview(control)
/* here is where the problem lies
Using NSView allows me to add the control to the view
BUT, it has no idea what "eventdelegate" is
and using **let temp: AnyClass? = control.superclass** doesn't help
basically this routine needs to accept ANY control type and know it has a eventDelegate parameter
*/
   ???.eventDelegate = self.window as? dsCONTROLDELEGATE
}
}

```

```swift
var tView : myTab() 
var myC1 : C1()
var myS1 : S1()

tView.addToPanel(panel:0,control:c1)
tView.addToPanel(panel0:control:s1)

```

note ALL controls end up pointing their eventdelegate back to the NSWindow that ultimately hosts the control. So a control on a TabPanel, does not delegate to the panel, but to the window itself

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [15 February 2022 23:31 UTC](https://ifnotnil.com/t/swift-adding-controls-to-a-tabpanel/2344/2 "2022-02-15T23:31:27Z")

</div>

I turned the problem around …

```auto
public func addToPanel(parent:NSTabView,panel:Int) {
		parent.tabViewItem(at: panel).view?.addSubview(self)
		self.eventDelegate = parent.window as? dsCONTROLDELEGATE
	}

	public func addToView(_ parent:dsVIEW?) {
		parent!.addSubview(self)
		self.eventDelegate = parent!.window as? dsCONTROLDELEGATE
	}

	public func addToView(_ parent:dsWINDOW) {
		addToView(parent.contentView! as? dsVIEW)
	}

```

so now it becomes

```auto
c1.addToPanel(parent:tView, panel:0)

```
