iOS-具有可变源/目标的Segue
我有一个iOS-具有可变源/目标的Segue,ios,objective-c,segue,uistoryboardsegue,Ios,Objective C,Segue,Uistoryboardsegue,我有一个UIViewController子类,它本身将子类化为许多自定义UIViewController。它包含一种检查身份验证信息的方法,如果身份验证失败,它应该转到特定的视图。为此,我正在考虑使用UIStoryboardSegue的*“segueWithIdentifier”*方法。问题是,我应该为destination参数指定什么,也就是说,我如何获得与我所需的destinationviewcontroller相关的UIViewController实例?我担心这并不容易,因为视图控制器的任
UIViewController
子类,它本身将子类化为许多自定义UIViewController
。它包含一种检查身份验证信息的方法,如果身份验证失败,它应该转到特定的视图。为此,我正在考虑使用UIStoryboardSegue的*“segueWithIdentifier”*
方法。问题是,我应该为destination
参数指定什么,也就是说,我如何获得与我所需的destinationviewcontroller
相关的UIViewController
实例?我担心这并不容易,因为视图控制器的任何子类都可能会转到不同的视图控制器,如果您想通过segue执行,那么所有的segue都会不同。我认为最好的解决方案是让您的子视图控制器决定触发哪个序列(显示/推送哪个视图)。
将这样的检查身份验证方法添加到父视图控制器
-(void)checkAuthentication
{
if (userAuthenticated)
{
[self userAuthenticatedMethod];
}
else
{
// if you want to go to the same view controller if user not authenticated you can
// perfoem segue like that:
[self performSegueWithIdentifier:@"failedSegue" sender:nil];
// but if it depends on the view controller you are in do it like that
[self userNotAuthenticatedMethod];
}
}
将此方法的声明添加到.h文件,并将空实现放在.m文件中:
//in .h
-(void)userAuthenticatedMethod;
//just if you need it
//-(void)userNotAuthenticatedMethod;
//in .m
-(void)userAuthenticatedMethod
{
//override in child
}
//just if you need it
//-(void)userNotAuthenticatedMethod
{
//override in child
}
现在,在每个子视图控制器中,您都需要实现userAuthenticatedMethod和userNotAuthenticatedMethod(如果需要)。
如果您想使用segue,只需执行以下操作:
-(void)userAuthenticatedMethod
{
[self performSegueWithIdentifier:@"yourSegue" sender:nil];
}
还可以通过编程方式将视图控制器添加到视图层次结构中。在此场景中,每个子视图控制器负责向视图层次结构添加另一个视图。
如果需要传递数据,您可以在每个子VC中覆盖prepareForSegue:method。在问题中添加故事板图片,以获得更清晰的信息