ios - 在 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。
解决方案
你可以做
if let secondViewController = viewControllerList.first as? HomeViewController {
secondViewController.loadViewIfNeeded() // not recommended
let res = secondViewController.children.first as! NestedVC
res.button..........
}
更好的是制作HomeVC
一个UIViewController
孩子并添加按钮和a UITableView
for 1直接访问而不是上面的嵌套访问
推荐阅读
- sql - 使用 SQL/Hive 查询得到 Country vs Country 的结果
- java - 如何使用线程重新调整 JFrame?
- java - Java将小数四舍五入为整数
- configuration - ansible中组中每个主机的不同变量值
- vue.js - 导入文件时未找到依赖项错误
- php - 重新索引和排序多维 PHP 数组
- python - Django Rest Framework,用户模型出错
- ruby-on-rails - 循环赛中粘性会话的 Nginx 配置更改
- amazon-web-services - 如何在 SNS 上向上传的新对象发送带有预签名 URL 的 S3 存储桶通知?
- asp.net-web-api - Asp.net core web api Localization DataAnnotation 自定义属性