如何从iOS的详细视图访问主表的视图 [英] How to access the master's tableview from detail view in iOS
问题描述
我的应用程式的一部分有一个嵌入的主要细节部分。每个详细视图都使用自定义 UIViewController
。当我改变这些UIViewControllers里面的东西的值时,我需要能够灰掉主 UITableViewController
中的一行表格。
我最近看到一个解决方案是使用 NSNotificationCenter
起泡任何更改,虽然这感觉有点不整洁..
另一个解决方案是使用委托?但我没有遇到任何示例解决方案或教程如何使用这在 Swift
?
ve也尝试通过导航回层次结构来访问表视图:
let navController = self.splitViewController!.viewControllers [0];
navController.tableView.reloadData()
错了,但我不知道如何访问主视图,或者即使是正确的方法。
哦,我试图调用 reloadData()
,因为在主视图中有一些逻辑,检查条件是否为灰色表行是适用的(我使用 Core数据
)
我已经看到,然而,一个更清洁和更未来证明的方式将使用委托协议:
协议DetailViewControllerDelegate:class {
func reloadTableView()
}
然后在DetailViewController类中添加一个委托属性,代理:
类DetailViewController:UIViewController {
weak var delegate:DetailViewControllerDelegate?
....
func reloadMasterTableView(){
delegate?.reloadTableView()
}
}
然后在您的MainViewController中实现委托方法:
extension class MainViewController:DetailViewControllerDelegate {
func reloadTableView(){
tableView.reloadData()
}
}
不要忘记在创建DetailViewController实例时设置委托:
let detailViewController = DetailViewController()
detailViewController.delegate = self
A portion of my app has an embedded master-detail section. Each detail view is using a custom
UIViewController
. When I change the value of something inside one of these UIViewControllers I need to be able to grey out one of the table rows in the masterUITableViewController
.The closest I have seen to a solution is to use
NSNotificationCenter
to bubble up any changes, though this feels a little untidy..Another solution is to use delegates? But I haven't come across any example solutions or tutorials in how to use this in
Swift
?I've also experimented just trying to access the table view by navigating back up the hierarchy:
let navController = self.splitViewController!.viewControllers[0]; navController.tableView.reloadData()
I know the example above is wrong, but I don't know how to access the master view that way, or even if it is the right approach.
Oh, I am trying to call
reloadData()
because in the master view there is some logic which checks the condition as to wether to grey out a table row is applicable (i'm usingCore Data
)解决方案I've seen that you figured this one out already. However a cleaner and more future proof way would be to use a delegate protocol:
protocol DetailViewControllerDelegate: class { func reloadTableView() }
Then add a delegate property to your DetailViewController class and implement the call to the delegate:
class DetailViewController: UIViewController { weak var delegate: DetailViewControllerDelegate? .... func reloadMasterTableView() { delegate?.reloadTableView() } }
And then in your MainViewController implement the delegate method:
extension class MainViewController: DetailViewControllerDelegate { func reloadTableView() { tableView.reloadData() } }
Don't forget to set the delegate on your DetailViewController instances when you create them:
let detailViewController = DetailViewController() detailViewController.delegate = self
这篇关于如何从iOS的详细视图访问主表的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!