【发布时间】:2020-12-23 20:37:09
【问题描述】:
class BibliothequesViewController: UIViewController {
static let sharedInstance = BibliothequesViewController()
var presentedBy: UIViewController?
}
我尝试在扩展程序中访问sharedInstance 和presentedBy:
extension BibliothequesViewController: UITableViewDelegate, UITableViewDataSource {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//Trying to present by using presentBy property
let vc = segue.destination as! presentedBy
// This throws this error: Use of undeclared type 'presentedBy'
let vc = segue.destination as! BibliothequesViewController.sharedInstance.presentedBy
// This throws this error: Static property 'sharedInstance' is not a member type of 'BibliothequesViewController'
}
}
第一个错误是有道理的。
第二个没有,因为我在应用程序的其他区域使用了BibliothequesViewController.sharedInstance.presentedBy,它工作正常。
重点是我想知道有没有办法在扩展中访问sharedInstance 或presentedBy?
【问题讨论】:
-
你需要在
as!之后指定类型而不是变量:let vc = segue.destination as! BibliothequesViewController -
但要回答您的问题,是的,您可以从扩展程序访问存储的属性。
标签: ios swift xcode uiviewcontroller tvos