首页 > 解决方案 > 在 PageViewController 中获取 ViewController 的 containerView

问题描述

我在向 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。

标签: iosswift

解决方案


你可以做

if let secondViewController = viewControllerList.first as? HomeViewController {

     secondViewController.loadViewIfNeeded()  // not recommended
     let res = secondViewController.children.first as! NestedVC
     res.button..........
}

更好的是制作HomeVC一个UIViewController孩子并添加按钮和a UITableViewfor 1直接访问而不是上面的嵌套访问


推荐阅读