在登录过程中隐藏Segue [英] Hiding a Segue on Login process
问题描述
我正在反对另一个问题:
按照上述职位描述的方法,我正在执行我的Auth令牌在Dashboard VC中,如果未设置,我将激活指向使用委托机制的LoginVC的段落,以便在操作完成后关闭视图。现在,问题是,在第一次运行之前,我会在LoginVC被显示之前得到了仪表板VC显示一段时间。我想显示登录屏幕立即隐藏(不知何故)Dashboard VC。换句话说,我希望用户不会注意到下面的事情。
任何想法?
这是我当前的故事板设置
,我在DashboardVC中检查我的凭据:
- (void)viewWillAppear :(BOOL)动画{
//检查是否设置了令牌,如果不触发登录屏幕
NSString * token = nil;
NSUserDefaults * userPref = [NSUserDefaults standardUserDefaults];
token = [userPref objectForKey:@AuthToken];
if(token == nil){
NSLog(@Token not present,Login required!);
[self performSegueWithIdentifier:@sLoginsender:nil];
[super viewWillAppear:NO];
}
// [super viewWillAppear:YES];
}
//如果Segue被Logout按钮触发,我们将擦除令牌,否则
//简单地执行登录,因为没有令牌在
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([sender tag] == 5){
NSLog(@ 用户选择注销);
//删除令牌
NSUserDefaults * userPref = [NSUserDefaults standardUserDefaults];
[userPref removeObjectForKey:@AuthToken];
[userPref synchronize];
NSLog(@Token removed);
}
if([segue.identifier isEqualToString:@sLogin]){
LoginViewController * livc = segue.destinationViewController;
livc.delegate = self; //对于代理方法
}
}
ElJay,我可能需要你的帮助:)
感谢大家!
我将把代码检查登录标记,并在 viewDidLoad
中执行segue,而不是等待视图出现,你说不想要。
if([[NSUserDefaults standardUserDefaults] boolForKey:@LoggedIn] == NO){
[self performSegueWithIdentifier :@Push LogInsender:self];
}
,如果您使用动画片段,那么您仍然可以看到rootviewController,所以我通过创建一个不使用动画的自定义segue来解决这个问题,所以loginController立即出现。
.h文件
@interface UIStoryboardSegueNoAnim:UIStoryboardSegue
@end
.m文件
#importUIStoryboardSegueNoAnim.h
@implementation UIStoryboardSegueNoAnim
- (void)perform {
[self.sourceViewController presentModalViewController:self.destinationViewController animated:NO];
}
@end
after finding a good solution about a missing Segue execution thanks to this post
I'm clashing against another problem: Following the approach described in the aforementioned post I'm performing the check for my auth token in the Dashboard VC and,if not set,I activate a segue pointing to the LoginVC exploiting the delegate mechanism in order to dismiss the view once the operation is completed. Now,the problem is that on the very first run I got the Dashboard VC show for a moment before the LoginVC gets displayed. I would like to show the Login screen immediately hiding (somehow) the Dashboard VC. In other words I want the user to not notice what's really going on underneath.
Any idea? This is my current storyboard setup
and I check for my credentials in the DashboardVC like that:
- (void)viewWillAppear:(BOOL)animated {
//check if the token is set,if not trigger the Login screen
NSString* token = nil;
NSUserDefaults* userPref = [NSUserDefaults standardUserDefaults];
token = [userPref objectForKey:@"AuthToken"];
if (token == nil) {
NSLog(@"Token not present,Login required!");
[self performSegueWithIdentifier:@"sLogin" sender:nil];
[super viewWillAppear:NO];
}
//[super viewWillAppear:YES];
}
// if the Segue was triggered by the "Logout" button we erase the token otherwise
// simply perform the login since there was no token at all
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([sender tag] == 5) {
NSLog(@"User selected Logout");
//remove the token
NSUserDefaults* userPref = [NSUserDefaults standardUserDefaults];
[userPref removeObjectForKey:@"AuthToken"];
[userPref synchronize];
NSLog(@"Token removed.");
}
if ([segue.identifier isEqualToString:@"sLogin"]) {
LoginViewController *livc = segue.destinationViewController;
livc.delegate = self; // For the delegate method
}
}
@ElJay,I might need your help again :)
Thanks everybody!
I would put the code that check for the login token and performs the segue in viewDidLoad
instead of waiting for the view to appear, which you say don't want.
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"LoggedIn"] == NO) {
[self performSegueWithIdentifier:@"Push LogIn" sender:self];
}
and also if you are using a segue with animation then you will probably still see the rootviewController, so I solved this by creating a custom segue that uses no animation so the login viewController appears immediately.
.h file
@interface UIStoryboardSegueNoAnim : UIStoryboardSegue
@end
.m file
#import "UIStoryboardSegueNoAnim.h"
@implementation UIStoryboardSegueNoAnim
- (void)perform {
[self.sourceViewController presentModalViewController:self.destinationViewController animated:NO];
}
@end
这篇关于在登录过程中隐藏Segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!