简体   繁体   中英

IOS Swift- Navigate to Home screen without Storyboard

I use firebase for authentication. Once user logs in, i want move to home screen with Tab controller.

App Delegate Function

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window?.makeKeyAndVisible()
    navToSignin()
    return true
}
func navToHome()
{
    print("navToHome")
    let customTab = CustomTabBar()
    window?.rootViewController = customTab
}
func navToSignin()
{
    let firstPage = SigninNewVC()
    window?.rootViewController = firstPage
}

SigninNewVC

override func viewWillAppear(animated: Bool)
{
    super.viewWillAppear(animated)

    if FIRAuth.auth()?.currentUser?.uid != nil
    {
        if let appDel = UIApplication.sharedApplication().delegate  as? AppDelegate
        {
            print("viewWillAppear")

            appDel.navToHome()
      //This func gets called but still does not navigate to Home VC
        }
    }
}

private func fbSignin(token: String)
{
    FIRAuth.auth()?.signInWithCustomToken(token) { (user, error) in

        if error == nil
        {
            if user != nil
            {
                if let appDel = UIApplication.sharedApplication().delegate as? AppDelegate
                {
                    JulehHUD.hideProgressHUD(self.view!)
                    appDel.jumpToHome()
                    //This part works fine when i login it navigates to Home Screen
                }
            }
        }
        else
        {
            print("Error:\n\(error)\n")
        }
    }
}

When i login for the first time it works fine I am able to Navigate to Home screen. But if i relaunch the App i not able to navigate to the home screen. It stays in the SigninNewVC though the print Statements print("viewWillAppear")& print("navToHome") are executed. Not sure what i am doing wrong. Below is my Tab bar class

class CustomTabBar: UITabBarController {

override func viewDidLoad()
{
    super.viewDidLoad()

    let homeController = HomeScreenVC()
    let newHomeTab = UINavigationController(rootViewController: homeController)
    newHomeTab.title = "Home"

    viewControllers = [newHomeTab]

}
}

Kindly let me know what i am doing wrong.

我认为您错误地启动了VC,应该使用CustomTabBar(nibName: nil, bundle:nil) ,并将子视图添加到它的loadView:

Adding below code to navToHome() & navToSignin() in App Delegate fixed the issue.

window = UIWindow(frame: UIScreen.mainScreen().bounds)    
window?.makeKeyAndVisible()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM