ios - 在 UIPageViewController 中获取 ViewController 的容器 View

标签 ios swift

我在向 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,我可以理解地得到以下错误:

Value of type 'HomeViewController' has no member '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。

最佳答案

你可以做到

if let secondViewController = viewControllerList.first as? HomeViewController {

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

更好的方法是使 HomeVC 成为 UIViewController 子级并添加按钮和 UITableView 以进行 1 次直接访问,而不是上面的嵌套访问

关于ios - 在 UIPageViewController 中获取 ViewController 的容器 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56099940/

相关文章:

ios - 使用自定义选项卡栏 Controller 类在两个 View Controller 之间传递数据

ios - 我们真的需要在 iOS 6 上进行应用内验证吗?

ios - 停止 Collection View 取消选择一个项目

ios - 非法配置。连接 "cell"无法将原型(prototype)对象作为其目标

ios - Storyboard禁用了我的 View

ios - 针对数组测试 TableView 的单元格中包含的值

ios - SKVideoNode 中的视频在 Xcode 模拟器中工作正常,但无法在大多数设备上播放

swift - 将包含多个文件路径的 Swift 字符串转换为 AppleScript 列表

ios - 选取图片并上传

swift - 在工作表内的表单中使用选取器不起作用