0
votes

i know how to set up a "normal" delegation between to ViewController, which are directly dependent. So if i say, i have to send a message from one view to its upper one, i know how to do this. But how do i set up a delegate, if there are more ViewControllers between the two?

So m let's say i have this setup according to the scheme:

ViewController1 -> ViewController2 -> ViewController3 (via ButtonPressed) (via ButtonPressed)

If i want to set a (for example) NSString in VC1 from VC2, i just write into the prepareForSegue, where i call the VC2:

VC2.delegate = self;

But what do i do when i want to transfer Data between VC3 and VC1 without having to change VC2? What do i set the delegate for, if it isn't "self"? I know my description is very bad, but i cannot describe it better.

I just want this: VC1 has one button and a label; the button opens VC2, which has one button too, but no label; this VC2Button shows VC3; And with a touch to the button in the VC3 i want to change the label in VC1. But without having to set a delegte to VC2 and then another to VC1, i know how this works.

Hope you get what i want.

Thanks in advance!

2
Check other sources of communication like NSNotification or KVO. - Raphael Oliveira
Thanks! The NSNotification solved it! - Gustl007

2 Answers

0
votes

You can either pass on the delegate from VC2 to VC3 like this:

Inside VC1:
VC2 *vc2 = [VC2 alloc] init];
vc2.delegate = self;

Inside VC2:
VC3 *vc3 = [VC3 alloc] init];
vc3.delegate = self.delegate;

Or you can use target-selector design pattern to achieve this.

Target-Selector way:

Inside VC1:
VC2 *vc2 = [VC2 alloc] initWithTarget:(id)iTarget andSelector:(SEL)iSelector];

Inside VC2: Pass on the target/selector received from VC1
VC3 *vc3 = [VC3 alloc] initWithTarget:(id)iTarget andSelector:(SEL)iSelector];
0
votes

NSNotificationCenter may help if you don't want to maintain too many pointers just to link two unrelated views together.