Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

daproject85

macrumors member
Original poster
Apr 13, 2011
37
0
Hi forum,

I wanted to know how you can set a view controller as another view controllers delegate? If you look at the screen shot below i have a series of tableviewcontrollers (each have their own custom class) and they segue from one to the other. The first one titled FORMATION has a method which retrieves data from a PLIST file. The THIRD view controller needs the same data. Now i know I can call the same method and just retrieve it, but i wanted to see if i can do this via delegates. I do not know how to set "FORMATION TABLEVIEW CONTROLLER" as the delegate.

I cannot do it in the "
Code:
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
" because i segue from the 1st to the 2nd tableview controller not to the third.

would i have to create an instance of the "Plays TABLE VIEW CONTROLLER" and then set the delegate that way?

please help

25qawc0.jpg
 
Last edited by a moderator:

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
Hi forum,

I wanted to know how you can set a view controller as another view controllers delegate? If you look at the screen shot below i have a series of tableviewcontrollers (each have their own custom class) and they segue from one to the other. The first one titled FORMATION has a method which retrieves data from a PLIST file. The THIRD view controller needs the same data. Now i know I can call the same method and just retrieve it, but i wanted to see if i can do this via delegates. I do not know how to set "FORMATION TABLEVIEW CONTROLLER" as the delegate.

I cannot do it in the "
Code:
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
" because i segue from the 1st to the 2nd tableview controller not to the third.

would i have to create an instance of the "Plays TABLE VIEW CONTROLLER" and then set the delegate that way?

please help

25qawc0.jpg


View controllers don't have a defined delegate. You can create a delegate property in your custom view controller in order to communicate between view controllers, but how you do that is up to you.

There are several things you could do.

In your case, your first view controller contains data that the second and third view controller needs to read. I would create a property dataSource in both your second and third view controller. (You can call it delegate if you want, but dataSource is a better description of the role it's serving.) In your first view controller's perpareForSegue method. I would set secondViewController.dataSource to self. Then in secondViewController's prepareForSegue method, set thirdViewController's dataSource to self.dataSource.

Alternately, you could simply add an array/dictionary property to all 3 view controllers. (Lets say it's an array called "storageArray") In each view controller's prepareForSegue, you'd just set the new view controller's array property to self.storageArray. Then all 3 view controllers would have shared ownership of the storage array.

As a third option, you could create a data container singleton object and store your plist data there. Then any object in your whole program that needs access to the data could get to it from the singleton.



Then in secondViewController's prepareForSegue
 

daproject85

macrumors member
Original poster
Apr 13, 2011
37
0
thanks alot Duncan for responding. A few questions for you:

1. about the first options as making the first view controller the delegate and segueing and just setting the first VC as delegate of second one and in the second VCs prepareforsegue method set the second as delegate to third, i did think of that but didn't know if that was correct. There is no way to cut the second one out (meaning the second one has no use for it)??

2. so it is good practice to just have the same method in 3rd and 1st VCs that just reads from a single model (my plist)?
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
thanks alot Duncan for responding. A few questions for you:

1. about the first options as making the first view controller the delegate and segueing and just setting the first VC as delegate of second one and in the second VCs prepareforsegue method set the second as delegate to third, i did think of that but didn't know if that was correct. There is no way to cut the second one out (meaning the second one has no use for it)??

2. so it is good practice to just have the same method in 3rd and 1st VCs that just reads from a single model (my plist)?

Disclaimer: I don't have a lot of experience with Storyboards. I still use XIB files for the most part.

It is my understanding that making a delegate and a protocol is Apple's suggested solution to passing data back from your second VC (view controller) to your first, but that this is a suggestion, not a formal rule. Whatever works.

Your need is slightly different. You're looking to pass a large data structure from VC to VC to VC. For that, you can simply share ownership of the data structure and pass it from one VC to the next to the next.

I would NOT use the same method to read the plist in all your VCs. That duplicates the data (bad) and requires that you spend the time re-reading the data in each VC (also bad). Better to use a data container singleton to hold the data structure and just get to it through the singleton. If you're not familiar with the singleton design pattern, you should read up on it. It's simple and easy to use, and is a great way of providing application-wide access to common data and methods.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.