I have an NSViewController which contains a button. This button is hooked up to another NSViewController in the storyboard so that when the button is pressed, the secondViewController is shown as a Sheet overlay. This works great and I get the look that I want, but a problem occurs when I subclass NSViewController and assign this subclass to the SECOND NSViewController. The first one works fine whether it has a custom class or not. If i do indeed assign a custom class to the second one, i leave the class exactly how it is created as shown below, but when I press the button, I get no error code (other than the annoying LLBD print in the console) and the app crashes. If someone could advise as to what I am doing incorrectly, or perhaps a way to get a better error log so i can troubleshoot a bit better, that would be appreciated. Please ask for more information / screenshots. Answers in both Swift and objective-c are fine.
import Cocoa
class CustomVC: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}
The below code is for my initial view controller. It contains 3 sets of data to fill the tableView depending on which segment of a NSSegmentedContoller is pressed. The sheet is going to be used to add a new item to the array - it will pop over the screen so that I can enter new details before saving them into one of the arrays. Hopefully this makes sense.
import Cocoa
class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
@IBOutlet weak var tableView: NSTableView!
var currentSegment = 0
var tableViewDataSite = ["Site 1", "Site 2", "Site 3", "Site 4", "Site 5"]
var tableViewDataSection = ["Section 1", "Section 2", "Section 3", "Section 4", "Section 5", "Section 6"]
var tableViewDataArea = ["Area 1", "Area 2", "Area 3", "Area 4"]
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "tableViewCellDidFinishEditingText:", name: NSControlTextDidEndEditingNotification, object: nil)
// Do any additional setup after loading the view.
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
//MARK: - TableView Datasource & Delegate
func numberOfRowsInTableView(tableView: NSTableView!) -> Int {
switch currentSegment {
case 0:
return tableViewDataSite.count
case 1:
return tableViewDataSection.count
case 2:
return tableViewDataArea.count
default:
return 0
}
}
func tableView(tableView: NSTableView!, objectValueForTableColumn tableColumn: NSTableColumn!, row: Int) -> AnyObject! {
switch currentSegment {
case 0:
return tableViewDataSite[row]
case 1:
return tableViewDataSection[row]
case 2:
return tableViewDataArea[row]
default:
return 0
}
}
func tableViewCellDidFinishEditingText(notification:NSNotification) {
let fieldEditor: NSTextView? = notification.userInfo!["NSFieldEditor"] as? NSTextView
switch currentSegment {
case 0:
tableViewDataSite.removeAtIndex(tableView.selectedRow)
tableViewDataSite.insert(fieldEditor!.textStorage.string, atIndex: tableView.selectedRow)
//Fetch the Site object from parse and change the name to fieldEditor!.textStorage.string
tableView.reloadData()
break
case 1:
tableViewDataSection.removeAtIndex(tableView.selectedRow)
tableViewDataSection.insert(fieldEditor!.textStorage.string, atIndex: tableView.selectedRow)
//Fetch the Site object from parse and change the name to fieldEditor!.textStorage.string
tableView.reloadData()
break
case 2:
tableViewDataArea.removeAtIndex(tableView.selectedRow)
tableViewDataArea.insert(fieldEditor!.textStorage.string, atIndex: tableView.selectedRow)
//Fetch the Site object from parse and change the name to fieldEditor!.textStorage.string
tableView.reloadData()
break
default:
break
}
}
//MARK: - IBActions
@IBAction func segemtnedControlDidChange(sender: AnyObject) {
let segmentedControl = sender as NSSegmentedControl
currentSegment = segmentedControl.selectedSegment
tableView.reloadData()
}