我在向PageViewController内的ViewController按钮添加动作时遇到问题。
到目前为止,对于特定的VC,我已经这样做了:
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
NotificationCenter.default.addObserver(self, selector: #selector(disableSwipe(_:)), name: Notification.Name(rawValue: "disableSwipe"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(enableSwipe(_:)), name: Notification.Name(rawValue: "enableSwipe"), object: nil)
if let secondViewController = viewControllerList.first as? HomeViewController {
self.setViewControllers([secondViewController], direction: .forward, animated: true, completion: nil)
secondViewController.iconPlus.action = #selector(addData(_:))
secondViewController.buttonReminder.addTarget(self, action: #selector(goToReminder(_:)), for: .touchUpInside)
secondViewController.buttonSettings.addTarget(self, action: #selector(goToSettings(_:)), for: .touchUpInside)
}
}
这种方法效果很好,但是现在我添加了一个ContainerView,其中包含带有按钮的TableViewController,我可以理解地收到以下错误:
“ HomeViewController”类型的值没有成员“ buttonReminder”
如何访问containerView的按钮以执行PageViewController的操作?
编辑,containerView-TableViewController:
class MenuTableViewController: UITableViewController {
@IBOutlet weak var buttonTimeline: UIButton!
@IBOutlet weak var buttonReminder: UIButton!
@IBOutlet weak var buttonLeasing: UIButton!
@IBOutlet weak var buttonSettings: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
}
secondViewController.loadViewIfNeeded()
let res = secondViewController.children.first as! MenuTableViewController
更清楚地说:我有一个HomeViewController,它是RootPageViewController的一部分。此HomeViewController包含一个“ ViewContainer”,以显示带有按钮的TableViewController。
答案 0 :(得分:1)
你可以
if let secondViewController = viewControllerList.first as? HomeViewController {
secondViewController.loadViewIfNeeded() // not recommended
let res = secondViewController.children.first as! NestedVC
res.button..........
}
更好的是使HomeVC
为UIViewController
的孩子,并添加按钮和UITableView
进行1次直接访问,而不是上述嵌套访问