简体   繁体   English

UISplitViewController,重用DetailViewController(Swift)

[英]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.

相关问题 更新UISplitViewController子类中的DetailViewController框架 - Update frame of DetailViewController in subclass of UISplitViewController 仅在UISplitViewController的DetailViewController中呈现视图控制器 - Presenting a View controller only in the DetailViewController of UISplitViewController 为什么detailViewController不在UISplitViewController.viewControllers中? - Why detailViewController is not in UISplitViewController.viewControllers anymore? 在UISplitViewController应用程序中从DetailViewController获取MasterViewController - getting the MasterViewController from the DetailViewController in a UISplitViewController app 在DetailViewController Swift中更改数据 - Change data in DetailViewController Swift 在UISplitviewCOntroller中将JSQMessagesViewController用作detailView时,KeyBoardToolBar只需要出现在DetailViewController中 - When using JSQMessagesViewController as detailView in UISplitviewCOntroller, KeyBoardToolBar needs to appear in DetailViewController only 当我在UISplitViewController中将iPad转到纵向模式时,调整detailViewController的pagecontrolview的大小 - Resize the pagecontrolview of detailViewController when i turn iPad to Portrait mode in UISplitViewController 从App Deligate UISplitViewController(MobileVLCKit)中的DetailViewController访问活动的Mediaplayer - Access active Mediaplayer from DetailViewController in App Deligate UISplitViewController (MobileVLCKit) UISplitViewController:DetailViewController 中的 titleView 在横向上消失,预期行为? - UISplitViewController: titleView in DetailViewController disappears on landscape orientation, intended behaviour? Swift-CoreData-DetailViewController-传递对象 - Swift - CoreData - DetailViewController - passing an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM