0

我的代码是

#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];

});
} 

但它不工作...

4

4 回答 4

1

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

    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() 
于 2018-02-19T09:17:23.880 回答
1

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

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
}
于 2018-02-19T09:26:12.860 回答
1

您要实现的是自动登录功能(如果用户已经登录,则不需要登录页面)。你正在做的事情是正确的,但你也需要管理这件事AppDelegate
设置根视图控制器后,您需要在代码中添加一行

        [[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];
    }

}
于 2018-02-19T09:26:57.703 回答
1

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

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

请按照以下步骤操作:

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

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

于 2018-02-19T09:28:18.573 回答