登录页面改变APPdelegate中的rootViewController

最初在appdelegate的didFinishLaunchingWithOptions中设置的rootVC是loginVC:

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = UIColor.white
        let loginSB = UIStoryboard(name: "Main", bundle: Bundle.main)
        let vc = loginSB.instantiateViewController(withIdentifier: "loginView")
       
        //根视图是登录页面
        window?.rootViewController = vc
        window?.makeKeyAndVisible()
       
        
        return true
    }

当点击登录的时候,需要将根视图换成tabbar或者其他视图:

swift:

 @IBAction func loginAction(_ sender: UIButton) {

        let mainTabCtrl: MainTabBarController! = MainTabBarController()
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        
        appDelegate.window?.rootViewController = mainTabCtrl
    }

oc

AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  UIViewController *rootViewController1 = appdelegate.window.rootViewController;


考虑下:

//查看api的时候 发现有个keywindow   不知道是否是上述中的window
UIWindow *window = [UIApplication sharedApplication].keyWindow;
   UIViewController *rootViewController = window.rootViewController;

你可能感兴趣的:(登录页面改变APPdelegate中的rootViewController)