0
votes

I’m new to xcode, and I’ve a problem with Viewcontrollers/delegates/protocols. I’d like to achieve the following in my code:

I have three ViewControllers.

Viewcontroller1 >segue> Viewcontroller2 >segue> Viewcontroller3

Viewcontroller1 is a TableViewController. To add a Row (via a button), there is a segue to ViewController2, where the user can select the type of the new row he wants to add (there are 4 different types of rows, which demand different kind of information from the user to be created). Depending on the joyce, he gets to (one of four different) ViewController3 (via segue), where he inserts the data for the new row, which will be added to the TableViewcontroller(VC1) (dismissal of ViewController3 and calling a „done and create“-Method).

To pass the data, I’m using delegates and protocols. In my approach ViewController1 is the delegate of ViewController3. Viewcontroller2 doesn’t really need to pass any data to the other Viewcontrollers. There are just 4 different buttons to start a segue to one of the 4 different ViewController3, so I thought that there’s no need to make it a delegate, (or give it a delegate).

My question:

Before the segue from ViewController2 to ViewController3, the ViewController3 (as far as I know) needs to be told, who his delegate is.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"ToViewcontroller3a“]) {

    UINavigationController *navCon = segue.destinationViewController;

    TLTableViewController3a *viewController = [navCon.viewControllers objectAtIndex:0];

    viewController.delegate = ??????;
}

}

  1. Can there be this „gap“ in the delegate hierarchy (so that the segue above doesn’t start from the delegate of ViewController3)?
  2. If yes, how can I tell ViewController3 before the segue from 2 to 3, that ViewController1 is its delegate, ("done and create"-method is implemented in ViewController1) to pass the data to ViewController1 to create the new row in the TableView?

In all the cases in tutorials and forums I found, the ViewController where the segue starts is always the delegate of the destinationViewcontroller. In my case its different.

1
would have been better if you had posted the screenshot of your storyboard? - Bikram Thapa
Thanks for your input, I can imagine this would help. Unfortunately I don't have enough reputation to post images. - user3680544

1 Answers

0
votes

All you want is to pass delegate controle to V3 from V1 .

So while initializig V2 in V1 put

V2.customdelegate1 = (id)self (customdelegate1 : you synthezise in V2)

Then in V2 while initializing V3 put

V3.customedelegate2 = V2.customdelegate1 (customdelegate2 : you synthezise in V3)

, let the V3 cuustom delgate function be

-(void)updateV1Tabe ;

kep this method in V1 and after entering details in V3 call this delegate from there it will fire in V 1.