1

The way I am designing my code, with several view controllers, that interact, I have the need to pass data between the view controllers. The way that I am doing it is to "pass a reference", make a pointer, to the target view controller in as a method argument like below:

-(void)aMethodToSetUpInterfaceElements:(UIViewController*)targetCV

Is there anything wrong with this, or do I need to watch out? It works well on a functional level, but what about the design?

OWolf
  • 5,012
  • 15
  • 58
  • 93
  • This post might interest you: http://stackoverflow.com/questions/13013476/ios-how-to-acheive-behavior-like-androids-startactivityforresult/13013847#13013847 – Greg Price Aug 05 '13 at 04:35

2 Answers2

2

There's nothing really wrong about this.

If it works for you, than it's ok. Just make sure to have a weak reference to the view controller, otherwise you could have a memory leak with retain cycle. Read this for more information: http://www.cocoawithlove.com/2009/07/rules-to-avoid-retain-cycles.html

Speaking about design, it's really depends of each case. I could't say preciselly because you didn't give more informations about the funcionality that you are trying to achieve.

Maybe if you want to make your code more generic, to be used in other places or projects, you might consider to perform a delegate to your view controller, but this depends of your views hierarchy and its design. To see more information about passing data between viewControllers, please see this: Passing Data between View Controllers

Community
  • 1
  • 1
Lucas Eduardo
  • 11,525
  • 5
  • 44
  • 49
  • Thanks for this great answer. I will read up! If these are easy to answer can I ask you a followup: I understand how to set up a weak reference using a property, but what about a weak reference to the reference established by the argument, is that weak? or do I need to declare it weak? What about if I wanted to make an ivar just to pass the reference to other methods in this class, and is that a bad idea? – OWolf Aug 05 '13 at 04:13
  • Again, it depends of what are you doing inside your method. If you are retaining the reference, some way, (ivar its a way) then you have to declare a weak reference of it first before passing as argument (like this: http://stackoverflow.com/questions/11603284/weak-references-inside-a-block) . But if you are just calling a method or something like that, that doesn't retain the reference, then you are ok. – Lucas Eduardo Aug 05 '13 at 12:58
0

I don't think it is a good idea to pass a view controller in order to transfer its data if you don't have enough reasons. I tend to exposing the minimal knowledge to another object.

For example, say you have two view controllers controllerA and controllerB.

  • If all you need is just to pass some data from controllerA to controllerB, you pass the data, which might be a NSData, a NSArray, a NSDictionary or your custom data structure. You should not pass the whole controllerA as an parameter to controllerB. Although you can access the data by using the controllerA.data getter, you have exposed too much knowledge about controllerA to controllerB, which may unconsciously ruin your design by increasing the coupling between controllerA and controllerB.
  • If you are aiming at handling the view transitions, that may be a good reason to pass a view controller as the parameter. So that you can make good use of its -presentViewController:animated:completion: to present a modal view, or push it into a navigation controller, or you probably want to get a reference to its view property and customize your own UI presentation.
liuyaodong
  • 2,547
  • 18
  • 31