I am trying to use a custom UIStoryboardSegue
to implement a transition between two view controllers. I can do this by subclassing UIStoryboardSegue
, and then setting this class in IB. However, I was looking at the docs which say:
我尝试使用自定义的UIStoryboardSegue来实现两个视图控制器之间的转换。我可以通过子类化UIStoryboardSegue,然后在IB中设置这个类来实现。
If your segue does not need to store additional information or provide anything other than a perform method, consider using the segueWithIdentifier:source:destination:performHandler: method instead.
如果您的segue不需要存储额外的信息或提供除执行方法以外的其他信息,那么可以考虑使用segueWithIdentifier:源:destination:performHandler:方法。
Implying that you don't need to create the custom subclass, just use the custom performHandler.
这意味着您不需要创建自定义子类,只需使用自定义performHandler。
I am confused as to where this code should go, and how I go about using it. Do I create the segue as normal in IB and then override that before it is fired (maybe in shouldPerformSegue: or similar). Elsewhere in apple's documentation it says:
我不知道这段代码应该放在哪里,以及如何使用它。在IB中,我是否像往常一样创建segue,然后在它被触发之前重写它(可能在shouldPerformSegue:或类似)?在苹果的其他文件中,它写道:
Your app never creates segue objects directly; they are always created on your behalf by iOS when a segue is triggered
你的应用程序从不直接创建segue对象;当触发segue时,iOS总是代表你创建它们
So I don't quite understand why they are then saying to instantiate a segue using a class creator method.
所以我不太明白他们为什么要用class creator方法实例化segue。
1 个解决方案
#1
18
The point of segueWithIdentifier:source:destination:performHandler:
的segueWithIdentifier:来源:目的地:performHandler:
- Provide an alternative to
UIViewController performSegueWithIdentifier:sender
in cases where you also want to create a custom transition, without creating a segue subclass. - 提供一个UIViewController performSegueWithIdentifier的替代方法:sender,在这种情况下,您还需要创建自定义转换,而无需创建segue子类。
- Vend a segue that can be used as the return for
segueForUnwindingToViewController:fromViewController:identifier
- Vend:一个可作为segueforunwindtoviewcontroller:fromViewController:identifier的返回的segue
As noted above, this approach is only viable for segues which you would call manually -- i.e. not for segues that would otherwise be triggered via IB triggers.
如上所述,此方法仅适用于您将手动调用的segue——也就是说,不适用于通过IB触发器触发的segue。
So, for example, if you have a segue that needs to be triggered after a certain timeout period (such as a custom lock-screen), you could use segueWithIdentifier:source:destination:performHandler:
to handle the custom transition.
因此,例如,如果您有一个需要在某个超时期间(如自定义锁屏)后触发的segue,您可以使用segueWithIdentifier:source:destination:performHandler:来处理自定义转换。
-(void)appTimeoutLockScreen
{
UIStoryboardSegue *segue =
[UIStoryboardSegue segueWithIdentifier:@"LockScreenSegue"
source:sourceVC
destination:destinationVC
performHandler:^{
// transition code that would
// normally go in the perform method
}];
// Dev is responsible for calling prepareForSegue and perform.
// Note, the order of calls for an IB triggered segue as well as
// a performSegueWithIdentifier segue is perform first, then
// prepareForSegue:sender. Manual segues need to inverse the call
// in order to ensure VC setup is finished before transition.
[self prepareForSegue:segue sender:self];
[segue perform];
}
Another practical use for the method is unwinding segues. Using a similar scenario to the previous example, we could use it to return a segue to transition from a lock screen back to the previous viewController:
该方法的另一个实际应用是展开segue。使用与前一个示例类似的场景,我们可以使用它返回一个segue,以从锁屏幕转换回前一个viewController:
-(UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController*)toVC
fromViewController:(UIViewController *)fmVC
identifier:(NSString *)identifier
{
UIStoryboardSegue *segue =
[UIStoryboardSegue segueWithIdentifier:@"FromLockScreenSegue"
source:fmVC
destination:toVC
performHandler:^{
// transition code
}];
return segue;
}
#1
18
The point of segueWithIdentifier:source:destination:performHandler:
的segueWithIdentifier:来源:目的地:performHandler:
- Provide an alternative to
UIViewController performSegueWithIdentifier:sender
in cases where you also want to create a custom transition, without creating a segue subclass. - 提供一个UIViewController performSegueWithIdentifier的替代方法:sender,在这种情况下,您还需要创建自定义转换,而无需创建segue子类。
- Vend a segue that can be used as the return for
segueForUnwindingToViewController:fromViewController:identifier
- Vend:一个可作为segueforunwindtoviewcontroller:fromViewController:identifier的返回的segue
As noted above, this approach is only viable for segues which you would call manually -- i.e. not for segues that would otherwise be triggered via IB triggers.
如上所述,此方法仅适用于您将手动调用的segue——也就是说,不适用于通过IB触发器触发的segue。
So, for example, if you have a segue that needs to be triggered after a certain timeout period (such as a custom lock-screen), you could use segueWithIdentifier:source:destination:performHandler:
to handle the custom transition.
因此,例如,如果您有一个需要在某个超时期间(如自定义锁屏)后触发的segue,您可以使用segueWithIdentifier:source:destination:performHandler:来处理自定义转换。
-(void)appTimeoutLockScreen
{
UIStoryboardSegue *segue =
[UIStoryboardSegue segueWithIdentifier:@"LockScreenSegue"
source:sourceVC
destination:destinationVC
performHandler:^{
// transition code that would
// normally go in the perform method
}];
// Dev is responsible for calling prepareForSegue and perform.
// Note, the order of calls for an IB triggered segue as well as
// a performSegueWithIdentifier segue is perform first, then
// prepareForSegue:sender. Manual segues need to inverse the call
// in order to ensure VC setup is finished before transition.
[self prepareForSegue:segue sender:self];
[segue perform];
}
Another practical use for the method is unwinding segues. Using a similar scenario to the previous example, we could use it to return a segue to transition from a lock screen back to the previous viewController:
该方法的另一个实际应用是展开segue。使用与前一个示例类似的场景,我们可以使用它返回一个segue,以从锁屏幕转换回前一个viewController:
-(UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController*)toVC
fromViewController:(UIViewController *)fmVC
identifier:(NSString *)identifier
{
UIStoryboardSegue *segue =
[UIStoryboardSegue segueWithIdentifier:@"FromLockScreenSegue"
source:fmVC
destination:toVC
performHandler:^{
// transition code
}];
return segue;
}