如何检查使用了哪个segue

non*_*uma 8 iphone objective-c ios uistoryboardsegue

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

gue*_*nis 8

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

- (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 = ...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

- (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 = ...
    }
}
Run Code Online (Sandbox Code Playgroud)