代表不会执行功能
Delegate won't execute function
我有一个带有容器视图的视图控制器,其中包含静态 UITableViewController
作为菜单。父视图控制器是 UIPageViewController
的一部分。
每当用户单击一行时,我想调用一个函数来从 PageViewController 执行 segue。为此,我在 MenuTableViewController
中添加了 didSelectRowAt
。 - 此函数工作正常,因为正在执行打印请求。
为了从 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)
....
这是你应该如何使用 delegate:
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()
}
}
您为 menuVc 创建另一个对象并将委托分配给它,它与情节提要中的 1 不同,因此在 PagerVCviewDidLoad
内部
let home = //
let menu = home.children.first as! MenuTableViewController
menu.delegate = self
我有一个带有容器视图的视图控制器,其中包含静态 UITableViewController
作为菜单。父视图控制器是 UIPageViewController
的一部分。
每当用户单击一行时,我想调用一个函数来从 PageViewController 执行 segue。为此,我在 MenuTableViewController
中添加了 didSelectRowAt
。 - 此函数工作正常,因为正在执行打印请求。
为了从 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)
....
这是你应该如何使用 delegate:
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()
}
}
您为 menuVc 创建另一个对象并将委托分配给它,它与情节提要中的 1 不同,因此在 PagerVCviewDidLoad
内部
let home = //
let menu = home.children.first as! MenuTableViewController
menu.delegate = self