我有一个带有容器视图的视图控制器,该容器视图包含一个静态UITableViewController
作为菜单。父视图控制器是UIPageViewController
的一部分。
每当用户单击行时,我都想调用一个函数来从PageViewController执行segue。为此,我在didSelectRowAt
内添加了MenuTableViewController
。 -由于正在执行打印请求,因此此功能工作正常。
要从PageViewController调用函数,我尝试使用委托,但不知何故该函数将无法执行。
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
let vc = storyboard?.instantiateViewController(withIdentifier: "menuView") as! MenuTableViewController
vc.delegate = self
...
}
func goToMap() {
performSegue(withIdentifier: "goMap", sender: self)
}
class MenuTableViewController: UITableViewController {
let defaults = UserDefaults.standard
weak var delegate: RootPageViewController?
}
extension MenuTableViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
if indexPath.row == 2 {
delegate?.goToMap()
}
tableView.deselectRow(at: indexPath, animated: true)
}
}
编辑:在PageViewController的viewDidLoad()中
let home = storyboard?.instantiateViewController(withIdentifier: "homeView") as! HomeViewController
home.loadViewIfNeeded()
let menu = home.children[1] as! MenuTableViewController
print("Children:", home.children)
menu.delegate = self
class RootPageViewController: UIPageViewController, UIPageViewControllerDataSource, UIScrollViewDelegate, UIViewControllerTransitioningDelegate {
lazy var viewControllerList:[UIViewController] = {
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc1 = sb.instantiateViewController(withIdentifier: "homeView")
let vc2 = sb.instantiateViewController(withIdentifier: "timelineView")
return [vc1, vc2]
}()
let defaults = UserDefaults.standard
let transition = CircularTransition()
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
let home = storyboard?.instantiateViewController(withIdentifier: "homeView") as! HomeViewController
home.delegate = self
print(home.delegate)
if let secondViewController = viewControllerList.first as? HomeViewController {
self.setViewControllers([secondViewController], direction: .forward, animated: true, completion: nil)
....
答案 0 :(得分:1)
您为menuVc创建另一个对象,并为其分配委托,它不同于情节提要中的1,因此在PagerVC的viewDidLoad
内部
let home = //
let menu = home.children.first as! MenuTableViewController
menu.delegate = self
答案 1 :(得分:0)
这是您应该使用委托的方式:
MenuTableViewController
protocol MenuTableViewControllerDelegate: class {
func menuTableViewController(menuTableViewController: MenuTableViewController, shouldGoToMap: Bool)
}
class MenuTableViewController: UITableViewController {
let defaults = UserDefaults.standard
weak var delegate: MenuTableViewControllerDelegate?
...
}
extension MenuTableViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
if indexPath.row == 2 {
delegate?.menuTableViewController(menuTableViewController: self, shouldGoToMap: true)
}
tableView.deselectRow(at: indexPath, animated: true)
}
}
RootPageViewController
class RootPageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
let vc = storyboard?.instantiateViewController(withIdentifier: "menuView") as! MenuTableViewController
vc.delegate = self
}
func goToMap() {
performSegue(withIdentifier: "goMap", sender: self)
}
}
extension RootPageViewController: MenuTableViewControllerDelegate {
func menuTableViewController(menuTableViewController: MenuTableViewController, shouldGoToMap: Bool) {
goToMap()
}
}