我创建了一个UISplitViewController
:
class EmployeesSplitViewController: UISplitViewController {
override func viewDidLoad() {
super.viewDidLoad()
let nav1 = UINavigationController()
let nav2 = UINavigationController()
let masterViewController = EmployeesList()
let detailViewController = EmployeeDetailsTableViewController()
nav1.viewControllers = [detailViewController]
nav2.viewControllers = [masterViewController]
detailViewController.view.backgroundColor = .white
self.viewControllers = [nav2, nav1]
self.maximumPrimaryColumnWidth = self.view.frame.width / 3
self.preferredDisplayMode = .allVisible
}
}
<{1>} EmployeeDetailsTableViewController
我有一个自定义提醒视图,在coreData中添加新员工,然后我想在reloadData
中调用EmployeesList
。我使用了爆燃模式:
protocol EmployeeDetailsTableViewControllerDelegate {
func refreshTable()
}
然后在EmployeeDetailsTableViewController
var delegate:EmployeeDetailsTableViewControllerDelegate?
func addButtonTapped(name: String, lastName: String, jobTitle: String) {
let coreManager = CoreDataManager()
coreManager.addNewEmployee(name: name, lastName: lastName, jobTitle: jobTitle)
alertView.removeFromSuperview()
blurEffectView.removeFromSuperview()
delegate?.refreshTable() //this is the delegate method
}
EmployeesList
中的
class EmployeesList: UITableViewController, NSFetchedResultsControllerDelegate, EmployeeDetailsTableViewControllerDelegate {
let employeeDetailsVC = EmployeeDetailsTableViewController()
override func viewDidLoad() {
super.viewDidLoad()
employeeDetailsVC.delegate = self
}
func refreshTable() {
print("RELOADING") //this never get called
self.tableView.reloadData()
}
}
refreshTable
永远不会被调用。我错过了什么?
更新:正如Phillip Mills正确指出的那样,我将委托分配给了错误的对象。我设法调用委托函数在masterViewController
上设置委托。现在唯一的问题是我还需要masterViewController
来调用masterViewController.tableView.reloadData()。如果我将其移到viewDidLoad
之外,我会收到以下错误:'NSGenericException', reason: 'The content view controller argument must be the root of its associated view controller hierarchy.'
class EmployeesSplitViewController: UISplitViewController, EmployeeDetailsTableViewControllerDelegate {
var masterViewController:EmployeesList!
override func viewDidLoad() {
super.viewDidLoad()
let nav1 = UINavigationController()
let nav2 = UINavigationController()
masterViewController = EmployeesList()
let detailViewController = EmployeeDetailsTableViewController()
detailViewController.delegate = self
nav1.viewControllers = [detailViewController]
nav2.viewControllers = [masterViewController]
detailViewController.view.backgroundColor = .white
self.viewControllers = [nav2, nav1]
self.maximumPrimaryColumnWidth = self.view.frame.width / 3
self.preferredDisplayMode = .allVisible
}
func refreshTable() {
masterViewController.tableView.reloadData()
}
}
答案 0 :(得分:0)
这两行...
let detailViewController = EmployeeDetailsTableViewController()
...和...
let employeeDetailsVC = EmployeeDetailsTableViewController()
...每个都创建一个对象。 也就是说,每个都创建一个不同的对象。
您指向detailViewController
的那个用于在屏幕上显示内容,但它是另一个分配了该代表的内容。
不要在EmployeesList
中创建第二个控制器,而是将masterViewController
设置为原始 EmployeeDetailsTableViewController
对象的代理。