设置根视图控制器不起作用?

Setting root view controller is not working?

我的密码是

#import "AppDelegate"

在 viewDidLoad 中

self.delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

收到回复后

//If server response is success make HomeViewController as Root VC
if ([[[_response objectForKey:@"Response"] objectForKey:@"status"]  isEqualToString:@"SUCCESS"]) {

dispatch_async(dispatch_get_main_queue(), ^{

//Make root view controller
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; //My storyboard name is Main.storyboard
HomeViewController * hvc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"HVC"];
self.delegate.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:hvc];
[self.delegate.window makeKeyAndVisible];

});
} 

但是没用...

SWIFT代码 在 Appdelegate Class 中实现两个方法,如下所示:-

    class AppDelegate: UIResponder, UIApplicationDelegate {
        var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

            application.statusBarStyle = .lightContent
            self.window = UIWindow(frame: UIScreen.main.bounds)
            self.window?.makeKeyAndVisible()

            return true
        }
        func showTabBar()  {

            let tabBarContoller = UIStoryboard(name: OTPStoryBoards.Booking, bundle: nil).instantiateViewController(withIdentifier: OTPViewControllers.TabBarController) as! UITabBarController
            self.window?.rootViewController = tabBarContoller
        }

        func showLogin()  {
            let loginVC = UIStoryboard(name: OTPStoryBoards.Authentication, bundle: nil).instantiateViewController(withIdentifier: OTPViewControllers.LoginVC) as! OTPLoginViewController
            let navigationControler = UINavigationController(rootViewController: loginVC)
            self.window?.rootViewController = navigationControler
        }
}

并且每当你想更改 root 时调用这些方法

let APP_DELEGATE    = UIApplication.shared.delegate as! AppDelegate

  APP_DELEGATE.showLogin() or APP_DELEGATE.showTabBar() 

在您的 LoginViewController 中,您需要跟踪用户成功登录的用户登录状态。为此,您可以在 UserDefaults 中存储一个 bool 变量,例如...

UserDefaults.standard.setValue(true, forKey: "isUserLoggedIn")
UserDefaults.standard.synchronize()

因此,当应用程序重新启动时,您需要检查用户是否已登录....并基于此您可以显示所需的屏幕...

if let isLoggedIn = UserDefaults.standard.value(forKey: "isUserLoggedIn") as? Bool, isLoggedIn == true {

   //Make root view controller
   UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
   HomeViewController * hvc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"HVC"];
   self.delegate.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:hvc];

} else {
    //show login view controller as user is not yet logged in
}

您要实现的是自动登录功能(如果用户已经登录,则不需要登录页面)。 你所做的是正确的,但你也需要从 AppDelegate 管理那个东西。
设置root view controller is

后需要在代码中添加一行
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isUserLoggedIn"]; 
        // PUT this line after  [self.delegate.window makeKeyAndVisible];

您可以在 AppDelegate 中创建方法来监视来自 UserDefault 的布尔变量。并导航到 vc1 -> vc2

将其放入 Appdelegate 并从 didFinishLuanchWithOptions

调用它
- (void) checkLoginStatusAndProceed {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isUserLoggedIn"] && [[NSUserDefaults standardUserDefaults] boolForKey:@"isUserLoggedIn"] == YES) {
        // Navigate to HomeViewController
        //Make root view controller
        UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; //My storyboard name is Main.storyboard
        HomeViewController * hvc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"HVC"];
        self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:hvc];
       [self.window makeKeyAndVisible];
    }

}

根据您的要求,您需要维护一个标志可能在 UserDefaults

注意:这只是伪代码,因为我不在办公桌前带着笔记本电脑

按照如下步骤操作:

  1. 创建用户默认对象并保存标志,例如 isFirstLaunch = false
  2. 将 HomeVC 设置为 root 后,将此标志更改为 true。
  3. didFinishLaunchingWithOptions 中的 AppDelegate 中,像

    一样再次检查此标志

    if UserDefaults.standard.bool(for:"isFirstLaunch"){ //显示登录 }别的{ //显示首页 }