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 = ??????;
}
}
- Can there be this „gap“ in the delegate hierarchy (so that the segue above doesn’t start from the delegate of ViewController3)?
- 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.