如何检查使用了哪个segue [英] How to check which segue was used

查看:78
本文介绍了如何检查使用了哪个segue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个segue,它们导致相同的 viewController 。有2个按钮使用2个segues连接到相同的 viewController 。在那个 viewController 中我需要检查点击了哪个按钮。所以实际上我需要检查使用/预制的segue。我如何在viewControllers类中检查这个?我知道有 prepareForSegue 方法,但我不能将它用于我的目的,因为我需要将 prepareForSegue 放入2个按钮所在的类,我不希望它在那里,但我希望它在 viewControllers 类中,因为我需要在该类中访问和设置一些变量。

I got two segue's which lead to the same viewController. There are 2 buttons which are connected to the same viewController using 2 segues. In that viewController I need to check which button was clicked. So actually I need to check which segue was used/preformed. How can I check this in the viewControllers class? I know there is the prepareForSegue method, but I cannot use this for my purpose because I need to put the prepareForSegue in the class where the 2 buttons are, and I don't want it there but I want it in the viewControllers class because I need to access and set some variables in that class.

推荐答案

您需要在第一个viewftroller的prepareforsegue方法中设置第二个viewcontroller的变量。这是如何完成的:

You need to set a variable of the second viewcontroller in the prepareforsegue method of first one. This is how it is done:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:segueIdentifier1])
    {
        SecondViewController *secondVC = (SecondViewController *)segue.destinationViewController;
        if(sender.tag == ...) // You can of course use something other than tag to identify the button
        {
            secondVC.identifyingProperty = ...
        }
        else if(sender.tag == ...)
        {
            secondVC.identifyingProperty = ...
        }
    }
}

然后你可以在第二个vc中查看该属性以了解你是如何到达那里的。如果您在故事板中为2个按钮创建了2个segue,则只有segue标识符足以设置相应的属性值。然后代码转为:

Then you can check that property in the second vc to understand how you came there. If you have created 2 segues in the storyboard for 2 buttons, then only segue identifier is enough to set the corresponding property value. Then code turns into this:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:segueIdentifier1])
    {
        SecondViewController *secondVC = (SecondViewController *)segue.destinationViewController;
        secondVC.identifyingProperty = ...
    }
    else if([segue.identifier isEqualToString:segueIdentifier2])
    {
        SecondViewController *secondVC = (SecondViewController *)segue.destinationViewController;
        secondVC.identifyingProperty = ...
    }
}

这篇关于如何检查使用了哪个segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆