Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISplitViewController Master / Detail communication

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.

like image 669
David Avatar asked Nov 06 '11 23:11

David


1 Answers

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):

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.

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).

like image 118
bryanmac Avatar answered Sep 28 '22 08:09

bryanmac