Storyboard - setting delegates

时光怂恿深爱的人放手 提交于 2019-11-27 11:47:44

问题


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; there is no destination that will respond to it.

If I click on a view controller object, I am able to see the class owner at the bottom, but as soon as I select the other view controller containing the outlet, the old selection is gone, so I cannot connect the two.

Is this Apple's way of saying we should only connect them programmatically?


回答1:


Correct. Set the delegate or other data in your prepareForSegue:sender: method. Here is an example:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Check the segue identifier
    if ([segue.identifier isEqualToString:@"showDetail"])
    {
        // Get a reference to your custom view controller
        CustomViewController *customViewController = segue.destinationViewController;

        // Set your custom view controller's delegate
        customViewController.delegate = self;
    }
}



回答2:


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;
    }
}


来源:https://stackoverflow.com/questions/8982128/storyboard-setting-delegates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!