xcode UISplitViewController 主/细节通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8031238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UISplitViewController Master / Detail communication
提问by David
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.
我刚刚开始使用 UISplitViewController - 我已经从各种教程中拼凑了一些代码,但是我无法看到如何将数据从 Master 发送到 Detail。我正在创建一个 RSS 阅读器只是为了向自己说明它应该如何工作。我已经解析了一个 RSS 提要,并用 UITableView 填充了 MasterViewController,但我一直在弄清楚如何在 detailViewController 的 UIWebView 中进行一行单击并加载相应的文章。任何提示表示赞赏。
采纳答案by bryanmac
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.
一个好的方法是使用委托。这允许一个视图调用另一个视图提供的回调。在这种情况下,详细视图依赖于现有的主视图,因此回调就可以了。我会避免让他们直接相互引用并直接读取彼此的数据。
What exactly does delegate do in xcode ios project?
Here's a tutorial with UISplitViewController that does just that (delegate between master/detail):
这是一个带有 UISplitViewController 的教程,它就是这样做的(在主/细节之间进行委托):
http://www.raywenderlich.com/1040/ipad-for-iphone-developers-101-uisplitview-tutorial
http://www.raywenderlich.com/1040/ipad-for-iphone-developers-101-uisplitview-tutorial
Specifically this section:
本节具体如下:
Hooking Up The Left With the Right
Time to play matchmaker and hook these two sides together.
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.
将左与右挂钩
是时候玩媒人并将这两个方面联系在一起了。
有许多不同的策略可以最好地实现这一点。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. 右视图控制器覆盖该属性以在属性更新时更新视图。这很好用,但我们将遵循 UISplitViewController 类参考中建议的方法——使用委托。基本思想是我们将用一个方法定义一个协议——“selectedBotChanged”。我们的右手边将实现这个方法,我们的左手边将接受一个想要了解这个的人的委托。
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).
另一种方法是拥有一个共享模型——有点像一个带有通知的单例来触发不同的视图,以根据来自通知的数据或查询模型以响应模型更改来更新自己。这有时在具有许多不相互依赖的视图并且只是以各种方式冒泡数据的应用程序中更好(这里不是这种情况 - 详细视图依赖于现有的主视图,因此委托很好)。