Reputation: 99
I have a program that needs to create dynamic tabs, and in each tab put a relevant data to that tab.
For testing purposes, I've tried to make a program that creates 1 tab dynamically and should display relevant data by calling a function.
It has 2 classes: TabsController
which is hooked up to the Tab Controller on the stoty board, and PageController
which is the class that should make the dynamic tabs.
The problem is, I do manage to make the dynamic page, but trying to update the label text doesn't update it - it stays with the "Test" value. I probably mess something up with the pages life cycles, but I can't understand what.
TabsController:
import Cocoa
class TabsController: NSTabViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear() {
super.viewWillAppear()
let page1 = PageController()
page1.changePageNum("1")
self.addChildViewController(page1)
}
}
PageController:
import Cocoa
class PageController: NSViewController {
var lblHeader = NSTextField(frame: NSMakeRect(20, 20, 120, 20))
override func viewDidLoad() {
super.viewDidLoad()
self.lblHeader.stringValue = "Test"
self.view.addSubview(self.lblHeader)
}
func changePageNum(_ pageNum: String) {
self.title = "Page \(pageNum)"
self.lblHeader.stringValue = "Page \(pageNum)"
}
}
And while I'm on that, I'm having a really hard time finding literature for mac os swift programming. Usually what I find is for ObjC or iOS oriented. Anyone knows a good source for this?
Thanks.
Upvotes: 1
Views: 125
Reputation: 3702
You are allocating a new PageController
every time your view appears. You'll eventually end up with hundreds of PageController
s. Allocate it as a variable instead and add it as child in viewDidLoad
:
class TabsController: NSTabViewController {
var pageController = PageController()
override func viewDidLoad() {
super.viewDidLoad()
pageControler.changePageNum("1")
self.addChildViewController(page1)
}
}
Ray Wenderlich has good Swift literature: https://store.raywenderlich.com/
Upvotes: 1