xcode 故事板 - 设置代表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8982128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:04:44  来源:igfitidea点击:

Storyboard - setting delegates

iosxcodexcode4delegatesuistoryboard

提问by Morrowless

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?

这是 Apple 的说法,我们应该只以编程方式连接它们吗?

回答by Marco

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

正确的。在您的prepareForSegue:sender:方法中设置委托或其他数据。下面是一个例子:

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

回答by kazi.munshimun

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 :

如果您的故事板转场目标视图控制器是 UIViewController,那么@Marco 的答案是正确的。但是如果你的目标视图控制器是 UINavigationViewController 那么你必须从 UINavigationViewController 获取 UIViewController :

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