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

时间:2018-02-19 08:51:11

标签: ios objective-c uinavigationcontroller pushviewcontroller rootviewcontroller

我的代码是

#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 个答案:

答案 0 :(得分: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() 

答案 1 :(得分:1)

LoginViewController中,您需要跟踪用户成功登录的用户登录状态。为此,您可以在UserDefaults中存储bool 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
}

答案 2 :(得分: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];
    }

}

答案 3 :(得分:1)

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

注意:它只是伪代码,因为我不在笔记本电脑的桌面上

按照以下步骤操作:

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

    如果UserDefaults.standard.bool(for:" isFirstLaunch"){        //显示登录     }其他{        //显示主页     }