【问题标题】:Present view controller before initial view controller is shown在显示初始视图控制器之前呈现视图控制器
【发布时间】:2016-11-21 11:45:35
【问题描述】:

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

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

我遇到的问题是,当应用程序启动时,它正在加载标签栏控制器,并从该控制器显示登录视图控制器,但它使用一个小时间窗口来显示标签栏控制器的视图屏幕,在登录视图之前。我需要从标签栏控制器直接呈现登录视图,但在那个小时间间隔内不显示标签栏控制器的视图,因为它不方便用户。

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

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

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

【问题讨论】:

  • 在将rootviewcontroller 设置为tabbarcontrollertabbarcontroller's rootview 之后显示登录视图控制器。你不应该在applicationdidbecomeactiveapplicationdidfinish launching 编码
  • 贴出你正在使用的代码我们会折射并帮助你,没有代码我们帮不了你很多

标签: ios objective-c


【解决方案1】:

在应用委托中,在 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];

【讨论】:

  • @Gustavo Amgarten 如果您觉得这很有帮助,请接受这个答案,以便其他人可以轻松找到。
  • 这个问题是我希望我的根视图控制器成为标签栏控制器,并从该控制器呈现登录控制器。这样做的原因是,如果用户身份验证成功,我只想关闭登录视图控制器
  • 你是使用storyboard还是通过代码来设置tabbarcontroller?
【解决方案2】:

当您在情节提要中有视图控制器时,您应该这样做。

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;

FirstViewControllerviewDidAppear 中,您应该检查用户是否登录并显示视图控制器。

 BOOL loginStatus = [[NSUserDefaults standardUserDefaults] boolForKey:@"isLoggedIn"];
 if(loginStatus == NO){

     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
     LoginViewController *loginViewcontroller = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
     [self presentViewController:loginViewcontroller animated:YES completion:nil];
 }

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

【讨论】:

    【解决方案3】:

    创建新的 .H .M

    (这是我的代码)

    yourController.h 和 yourController.m

    然后打开

    你的控制器.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];
    }
    

    //在你想调用的地方调用这样的方法 //在应用代理中

    UIView *rectView;
    
    
     - (void)applicationWillEnterForeground:(UIApplication *)application
    {
    
    
        [rectView removeFromSuperview];
        rectView = [[VurifyAppValidation alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT)];
    [self.window addSubview:rectView];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 2015-07-16
      • 2019-07-28
      • 1970-01-01
      相关资源
      最近更新 更多