登录后为什么我没有在Swift中将HomeViewController用作Rootviewcontroller?

问题描述 投票:0回答:1

appdelegate,我正在获取UserId ..表示我正在登录,但是当我第二次运行时,我没有得到homeviewcontroller作为rootviewcontroller为什么?它仍然显示phonenumviewcontroller

navigationcontroller -> phonenumviewcontroller -> registrationiewcontroller -> homeviewcontroller

在情节提要中,导航控制器是initialviewcontroller

在registrationviewcontroller中,我得到userId,我已将其保存在钥匙串中。我没有signout按钮,所以我在registrationiewcontroller中编写了如下代码:

 do {
       let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String:AnyObject]
       print("terms and condition JSON \(json)")
       let jsonObj: String = json["Success"] as? String ?? ""
       if jsonObj == "true" {
        let userID: String=jsonObj?["userId"] as? String ?? ""

        DispatchQueue.main.async {
           KeychainWrapper.standard.set(userID, forKey: "USERID")
           let viewController = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController;
            UserDefaults.standard.set("NoLogout", forKey:"Signout")
            self.navigationController?.pushViewController(viewController, animated: true);
                    }
                    }
         }

没有注销按钮,因此在registrationviewcontroller中添加了此代码

  UserDefaults.standard.set("NoLogout", forKey:"Signout")

appdelegate中的此代码

:获得userId但仍然没有homeviewcontroller作为rootviewcontroller,为什么只有phonenumviewcontroller出现?
  var savedUserId: String?

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

    savedUserId = KeychainWrapper.standard.string(forKey: "USERID")
    KeychainWrapper.standard.set(savedUserId ?? "", forKey: "PersonalID")
    print("appdelegate userid \(savedUserId)")
    logOutString = UserDefaults.standard.string(forKey: "Signout") as NSString? ?? "" as NSString
    if logOutString.isEqual(to: "NoLogout") {
        self.window = UIWindow(frame: UIScreen.main.bounds)
        let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
        let viewcontroller = storyBoard.instantiateViewController(withIdentifier: "HomeViewController")
        let navigationController = UINavigationController(rootViewController: viewcontroller)
        self.window?.rootViewController = navigationController
        self.window?.makeKeyAndVisible()
    }
    else {
    }
    return true
}

一旦完成注册,我需要rootviewcontroller作为homeviewcontroller ...如何获得它,请提供代码帮助我

在appdelegate中,我正在获取UserId。.表示我已经登录,但是当我第二次运行时,我没有得到homeviewcontroller作为rootviewcontroller,为什么?它仍然显示phonenumviewcontroller ...

ios swift rootviewcontroller
1个回答
0
投票

根据我收集的内容,这是您需要做的:在AppDelegate中:

© www.soinside.com 2019 - 2024. All rights reserved.