显示初始视图控制器之前的当前视图控制器

时间:2016-11-21 11:45:36

标签: ios objective-c

我有一个具有以下结构的应用程序:

主视图控制器是一个标签栏视图控制器,有4个选项卡。 用户只有在登录时才能访问这些视图控制器。当应用程序启动时,它会加载它的初始视图控制器(标签栏一个),然后检查用户是否经过身份验证。如果不是,我会提供一个登录视图控制器。

我遇到的问题是,当应用程序启动时,它会加载标签栏控制器,并从该控制器显示登录视图控制器,但它会显示一个小时间窗口,显示在登录视图之前,标签栏控制器在屏幕上的视图。我需要从标签栏控制器直接显示登录视图,但不能在很短的时间间隔内显示标签栏控制器的视图,因为它不是用户友好的。

我在stackoverflow上阅读了关于在没有动画的情况下展示新视图控制器的一些答案,以及我正在做的事情,但问题仍然存在。

我希望我在这个问题上已经足够清楚,如果您需要更多信息,请告诉我。

编辑:我在applicationDidBecomeActive:上的applicationDelegate.m上展示了登录视图控制器

3 个答案:

答案 0 :(得分:1)

在app delegate中,在didFinishLaunchingWithOptions中,

如果是否登录,则

self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    UIViewController *viewController =[storyboard instantiateViewControllerWithIdentifier:@"your identifier"];

    self.window.rootViewController = viewController;
    [self.window makeKeyAndVisible];

答案 1 :(得分:1)

当你在故事板中有视图控制器时,你应该这样。

self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

FirstViewController *firstviewController = [storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];
SecondViewController *secondviewController = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];

firstviewController.tabBarItem.image = [UIImage imageNamed:@"24-around-7"];
firstviewController.tabBarItem.title = @"First";
secondviewController.tabBarItem.title = @"Second";
secondviewController.tabBarItem.image = [UIImage imageNamed:@"60-around-7"];
UITabBarController *tabBarController = [[UITabBarController alloc]init];
tabBarController.viewControllers = @[firstviewController,secondviewController];

self.window.rootViewController = tabBarController;

FirstViewController的{​​{1}}中,您应该检查用户是否登录并显示视图控制器。

viewDidAppear

成功登录后,只需关闭登录视图控制器即可。如果用户注销只显示loginviewcontroller

答案 2 :(得分:0)

创建新的.H .M

(这是我的代码)

yourController.h和yourController.m

然后打开

yourcontroller.m

- (id)initWithFrame:(CGRect)frame

{
    self = [super initWithFrame:frame];
if (self)
{
    NSLog(@"initWithFrame");

    [self setupPinPopUp];
}
return self;
}

-(void)setupPinPopUp{

UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
blurEffectView.frame = CGRectMake(0, 66, kSCREEN_WIDTH, kSCREEN_HEIGHT-66);
blurEffectView.userInteractionEnabled = TRUE;

UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
                                        action:@selector(handleSingleTap:)];
[blurEffectView addGestureRecognizer:singleFingerTap];

blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:blurEffectView];

UIView *popUpMainView = [[UIView alloc]initWithFrame:CGRectMake(kSCREEN_WIDTH/2-150, kSCREEN_HEIGHT/2-160, 300, 270)];
popUpMainView.backgroundColor = Clear;
[self addSubview:popUpMainView];


UIView *popUpInsideView = [[UIView alloc]initWithFrame:CGRectMake(kSCREEN_WIDTH/2-150, kSCREEN_HEIGHT/2-102, 300, 210)];
popUpInsideView.backgroundColor = White;
popUpInsideView.layer.cornerRadius = 2.0;
popUpInsideView.clipsToBounds = TRUE;
[self addSubview:popUpInsideView];


UIImageView *imgCircleView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 0, 100, 100)];
imgCircleView.layer.cornerRadius = 50;
imgCircleView.backgroundColor = White;
imgCircleView.clipsToBounds = TRUE;
[popUpMainView addSubview:imgCircleView];


UIImageView *imgInnerCircleView = [[UIImageView alloc]initWithFrame:CGRectMake(25, 8, 50, 50)];
imgInnerCircleView.backgroundColor = Clear;
imgInnerCircleView.image = [UIImage imageNamed:@"support"];
[imgCircleView addSubview:imgInnerCircleView];


UILabel *lblHeading = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, popUpMainView.frame.size.width, 45)];
lblHeading.text = @"CUSTOMER SUPPORT";
lblHeading.numberOfLines = 0;
lblHeading.textColor = Black;
lblHeading.textAlignment = NSTextAlignmentCenter;
lblHeading.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:21];
[popUpInsideView addSubview:lblHeading];


    UIButton *btnPhoneNumber = [[UIButton alloc]initWithFrame:CGRectMake(0, lblHeading.frame.size.height+lblHeading.frame.origin.y+10, popUpMainView.frame.size.width, 45)];
    [btnPhoneNumber setTitle:@"18002345678" forState:UIControlStateNormal];
   [btnPhoneNumber setTitleColor:Black forState:UIControlStateNormal];
    btnPhoneNumber.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    btnPhoneNumber.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
    [btnPhoneNumber addTarget:self action:@selector(callSupport:) forControlEvents:UIControlEventTouchUpInside];
    [popUpInsideView addSubview:btnPhoneNumber];


UIButton *btnEmail = [[UIButton alloc]initWithFrame:CGRectMake(0, btnPhoneNumber.frame.size.height+btnPhoneNumber.frame.origin.y+10, popUpMainView.frame.size.width, 45)];
[btnEmail setTitle:@"support@vurify.com" forState:UIControlStateNormal];
[btnEmail setTitleColor:Black forState:UIControlStateNormal];
btnEmail.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
btnEmail.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
 [btnEmail addTarget:self action:@selector(emailSupport:) forControlEvents:UIControlEventTouchUpInside];
[popUpInsideView addSubview:btnEmail];

}

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {

[self removeFromSuperview];

}


-(IBAction)callSupport:(id)sender{

NSString *phoneNumber = [@"tel://" stringByAppendingString:@"180023456789"];
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];


[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber] options:@{} completionHandler:^(BOOL success)
 {
 if (success) {
     NSLog(@"Opened url");
 }
 }];

}


-(IBAction)emailSupport:(id)sender{

 [self removeFromSuperview];
}

//在任何你想要调用的地方调用这样的方法 // IN APP DELEGATE

UIView *rectView;


 - (void)applicationWillEnterForeground:(UIApplication *)application
{


    [rectView removeFromSuperview];
    rectView = [[VurifyAppValidation alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT)];
[self.window addSubview:rectView];
}