[英]UISplitViewController, reuse DetailViewController (Swift)
Setup: Create a boilerplate iOS Master-Detail app in Xcode and run it. 设置:在Xcode中创建样板iOS Master-Detail应用并运行它。 Everytime you click on a master item it re-creates the detailVC and configures it to display the new data.
每次单击主项目时,它都会重新创建detailVC并将其配置为显示新数据。
Premise: Often this is what I want, but other times it does little more than change the text in a single label. 前提:通常这就是我想要的,但是有时它所做的只是更改单个标签中的文本。 Doesn't it make more sense to re-use the existing DetailVC?
重用现有的DetailVC更加有意义吗? (at least in this case)
(至少在这种情况下)
So what's the best way to do this? 那么最好的方法是什么?
Contemplation: When I look at the boilerplate code I see the MasterVC creates class scoped var for the detailVC. 沉思:当我查看样板代码时,我看到MasterVC为detailVC创建了类范围的var。
var detailViewController: DetailViewController? = nil
It sets this value in viewDidLoad
, but it then never uses it for anything. 它在
viewDidLoad
设置此值,但是从不将其用于任何东西。 Huh? ?? In
prepare(for:sender:)
we get this code 在
prepare(for:sender:)
我们获得此代码
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
which creates a new instance of the DetailViewController
. 这将创建一个
DetailViewController
的新实例。 If you put a break point in after it's created and compare it to the class var detailViewController
you can see they are different. 如果在创建断点后将其插入,并将其与var
detailViewController
类进行比较,您会发现它们是不同的。
The UIStoryboardSegue
is creating my new DetailViewController
which can easily be confirmed by looking at the documentation. UIStoryboardSegue
正在创建我的新DetailViewController
,可以通过查看文档轻松地对其进行确认。
You do not create segue objects directly.
您不能直接创建segue对象。 Instead, the storyboard runtime creates them when it must perform a segue between two view controllers.
相反,情节提要运行时必须在两个视图控制器之间执行segue时才创建它们。
I could remove the segue on row select and manually call the method on my detailVC and that's probably the easiest, but segues are so purty and visual in IB. 我可以删除行选择中的segue并在我的detailVC上手动调用该方法,这可能是最简单的,但是IB中的segue如此纯净而直观。 I could could probably create a custom segue.
我可能可以创建自定义序列。 What else could I do and is there a clear "best" way?
我还能做什么,并且有明确的“最佳”方法吗?
In case someone else has this question, here is my solution until someone comes up with something better. 如果有人有这个问题,这是我的解决方案,直到有人提出更好的建议。
Go to Interface Builder and remove the showDetail
segue which goes from the master tableview to the detailVC. 转到Interface Builder并删除从主tableview到detailVC的
showDetail
segue。 Then go into the MasterViewController class and remove the prepare(for:sender:)
function. 然后进入MasterViewController类,并删除
prepare(for:sender:)
函数。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let object = objects[indexPath.row] as! NSDate
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
Create a new function in Table View area (this new function is part of UITableViewDelegate
) 在“表视图”区域中创建一个新函数(此新函数是
UITableViewDelegate
一部分)
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
detailViewController?.detailItem = object
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.