Storyboard - setting delegates

前端 未结 2 1041
无人及你
无人及你 2020-12-13 20:06

Before storyboards I was able to set delegates and datasources just by dragging an outlet to a class. With storyboards, I cannot drag the outlet to another view controller;

2条回答
  •  一生所求
    2020-12-13 20:50

    If your storyboard segue destination View Controller is an UIViewController then @Marco answer is right. But if your destination View Controller is a UINavigationViewController then you have to get the UIViewController from UINavigationViewController :

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        // Check the segue identifier
        if ([segue.identifier isEqualToString:@"chooseCategoryType"])
        {
            // Get a reference of your custom view controller if your segue connection is an UIViewController
            // CustomViewController *customViewController = segue.destinationViewController;
            // Get a reference of your custom view controller from navigation view controller if your segue connection is an UINavigationViewController
            CustomViewController *customViewController = [[[segue destinationViewController] viewControllers] objectAtIndex:0];
    
            // Set your custom view controller's delegate
            customViewController.delegate = self;
        }
    }
    

提交回复
热议问题