我正在使用此库:swift-slide-menu
有没有办法以编程方式更改视图?我使用相同的viewcontroller在tableview中显示两个不同的数据集。我在菜单中有两个按钮,每个按钮对应一个数据集。单击button1时,我转到ListVC,一切正常。当点击button2时,我转到一个空的ViewController,它设置数据集的url并立即切换到ListVC。
当我执行segue时,菜单按钮不会显示在ListVC中。这可能是因为我在segue时没有向BaseViewController添加一个childView。但有没有办法在我的空ViewController中添加一个代码,将ListVC添加为childView,而不是正常的segue?
PS。 我试图阻止在两个不同的viewcontrollers中使用相同的代码,并在storyboard中拥有相同vc的2个副本。
class ViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
addChildView("AllDatasetID", titleOfChildren: "All data", iconName: "icon1") //ListVC
addChildView("FilteredDatasetID", titleOfChildren: "My data", iconName: "icon2") //EmptyVC that segues to ListVC
}
}
class EmptyVC: UIViewController {
override func viewDidAppear(_ animated: Bool) {
self.performSegue(withIdentifier: "toListVC", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toListVC" {
if let destinationVC = segue.destination as? ListVC {
destinationVC.theUrl = Settings.myFilterUrl
}
}
}
}
答案 0 :(得分:0)
解决方案是从ListVC内部访问存储当前视图标题的菜单中的变量。菜单子类BaseViewController和几个变量可通过navigationController从BaseViewController访问。
class ViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
addChildView("ListVCScreenID", titleOfChildren: "My datapoints", iconName: "icon1")
addChildView("ListVCScreenID", titleOfChildren: "All datapoints", iconName: "icon2")
}
}
class ListVC: UIViewController {
var theUrl: String?
override func viewDidLoad() {
super.viewDidLoad()
let vc = self.navigationController?.viewControllers
var counter = 0
for v in vc! {
if v as? ViewController != nil {
print("title is: \(v.title)")
if(v.title == "My datapoints") {
theUrl = "getMyData"
}
else {
theUrl = "getAllData"
}
}
}
}
}