XCool
XCool

Reputation: 996

Proper Way to Assign Delegate

Here's my situation.

I have ViewControllerA and it is capable of loading ViewControllerB thru presentModalViewController. Now, ViewControllerB can later on push ViewControllerC into view. Both ViewControllerB and ViewControllerC are the same class. How do I assign ViewControllerA as the delegate when I push ViewControllerC into view?

Currently, I'm assigning ViewControllerB as ViewControllerC's delegate, which then calls ViewControllerA and I feel that this is not so right. Chain of command: ViewControllerC -> ViewControllerB -> ViewControllerA

Thanks.

Upvotes: 1

Views: 92

Answers (2)

richerd
richerd

Reputation: 1247

I feel like the design is a little clumsy. Instead of passing the delegate back down the chain why not just assign the delegate of both ViewControllerB and ViewControllerC to viewControllerA (since they are the same class). You shouldn't have to pass up the chain unless C needs to communicate with B.

Before presenting the ViewControllerB:

b.delegate = self
[self presentModalViewController:b animated:YES];

in ViewControllerB before pushing ViewControllerC

c.delegate = self.delgate 
[self.navigationController pushViewController:c animated:YES]

in this way both B->A and also C->A

Upvotes: 1

Mundi
Mundi

Reputation: 80265

Your design (C->B->A) is not so bad because it follows the logic of the UI flow. (You are not jumping from C to A directly.) Still, you could assign A as delegate as well if you prefer. Just make sure A stays in memory (with the modal view controllers that should be automatic).

Upvotes: 0

Related Questions