本文介绍了UISplitViewController主/详细通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始玩UISplitViewController - 我已经拼凑在一起来自各种教程的一些代码,但我无法看到如何从主机发送数据到细节。我创建一个RSS阅读器只是为了向自己说明它应该如何工作。我解析了一个RSS源并使用UITableView填充MasterViewController,但我坚持想办法如何在一个行点击并加载相应的文章在一个UIWebView在detailViewController。

I just started playing with the UISplitViewController - I've cobbled together some code from various tutorials, but I'm having trouble seeing how to send data from the Master to the Detail. I'm creating an RSS reader just to illustrate to myself how it should work. I've parsed an RSS feed and populated the MasterViewController with a UITableView, but I'm stuck figuring out how to take a row click and load the corresponding article in a UIWebView in the detailViewController. Any tips are appreciated.

推荐答案

一个好的方法是使用委托。这允许一个视图调用由另一个提供的回调。在这种情况下,详细信息视图依赖于主存在,所以它有回调是罚款。我会避免让他们直接引用彼此,直接读取彼此的数据。

A good approach is to use delegates. That allows one view to call a callback provide by the other. In this case the detail view relies on the master existing so having it callback is fine. I would avoid letting them have direct references to each other and reading each others data directly.

具体本节:

时间来玩火柴人
这两边一起。

Time to play matchmaker and hook these two sides together.

有许多不同的策略,如何
最好完成这个。在分割视图应用程序模板中,
给左视图控制器一个指向右视图控制器的指针,
和左视图控制器在右视图上设置一个属性
控制器。当属性
更新时,右视图控制器
覆盖更新视图的属性。这工作正常,但我们将遵循$ UBI方法建议在UISplitViewController类引用这里 - 使用
委托。基本思想是我们要使用
单一方法定义一个协议 - selectedBotChanged。我们的右边将
实现这个方法,我们的左手边会接受一个委托$ b $

There are many different strategies for how to best accomplish this. In the Split View Application template they give the left view controller a pointer to the right view controller, and the left view controller sets a property on the right view controller when a row gets selected. The right view controller overrides the property to update the view when the property is updated. That works fine, but we’re going to follow the approach suggested in the UISplitViewController class reference here – use delegates. The basic idea is we’re going to define a protocol with a single method – "selectedBotChanged." Our right hand side will implement this method, and our left hand side will accept a delegate of somebody who wants to know about this.

另一种方法是使用共享模型 - 类似单例的通知以触​​发不同的视图基于来自通知的数据或响应于模型改变来查询模型来更新自身。这有时更好在一个应用程序,许多视图,不依赖于彼此,只是冒泡数据以各种方式(这不是这种情况 - 细节视图依赖于主存在,所以代理是很好的)。

Another approach would be to have a shared model - sort of like a singleton with notifications to trigger different views to update themselves based on either the data from the notification or querying the model in reaction to a model changes. This is sometimes better in an app with many views that don't rely on each other and just bubble up data in various ways (which is not the case here - the detail view relies on the master existing so a delegate is fine).

这篇关于UISplitViewController主/详细通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 12:01