UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
DetailViewController * controller = (DetailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"DetailView"];
controller.delegate = self;
[self presentViewController:controller animated:YES completion:nil];
iOS, Set delegate for scene without using segue
-
18-03-2022 - |
Question
I am trying to navigate to a scene without using a segue. The code I am using looks like this:
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
MyViewController * controller = (MyViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
[self presentViewController:controller animated:YES completion:nil];
My problem is setting the delegate for this scene. Usually I would do the following:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Check the segue identifier
if ([[segue identifier] isEqualToString:@"showDetail"])
{
[[segue destinationViewController] setDelegate:self];
}
}
But I do not want to use segues. Can anyone please tell me how I could set the delegate for the scene?
Thanks
Solution
OTHER TIPS
YOu can set the delegate for the scene you are loading.
Your "destinationViewController" will have a method "ViewWillAppear". There you can set the delegate to self.
as you already have a reference to the new controller you don't need to implement the prepareForSegue:sender:
method, after the line:
MyViewController * controller = (MyViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
just add:
[controller setDelegate:self];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow