ios - 尝试弹出到不存在的 View Controller swift 5.1 Xcode iOS

标签 ios swift xcode

我正在开发应用程序。当我单击注销按钮时,它向我显示此错误尝试弹出到不存在的 View Controller

这是我的 Storyboard屏幕 **All Screen**

**login SignUp Screen**

**profile screen with tabbar & navigationController**

这是我的 AppDelegate 代码

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    if #available(iOS 13.0, *)
    {
    //do nothing we will have a code in SceneceDelegate for this
    }
    else {

            makeRoot()
    }
    FirebaseApp.configure()
    return true
}

func makeRoot()
{
    let defaults = UserDefaults.standard

    if defaults.bool(forKey: "isLogin") == true
    {
        let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let VC = mainStoryboard.instantiateViewController(withIdentifier: "RootTabBarC") as! RootTabBarC
        let centerNavVC = UINavigationController(rootViewController: VC)
        //centerNavVC.isNavigationBarHidden = true
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = centerNavVC
        self.window?.makeKeyAndVisible()

    }
    else
    {
        let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let VC = mainStoryboard.instantiateViewController(withIdentifier: "LoginSignUpVC") as! LoginSignUpVC
        let centerNavVC = UINavigationController(rootViewController: VC)
        //centerNavVC.isNavigationBarHidden = true
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = centerNavVC
        self.window?.makeKeyAndVisible()
    }



}

这是我的 SceneDelagate 代码

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let windowScene = (scene as? UIWindowScene) else { return }

    let defaults = UserDefaults.standard

    if defaults.bool(forKey: "isLogin") == true
    {
        //guard let windowScene = (scene as? UIWindowScene) else { return }


        self.window = UIWindow(windowScene: windowScene)
        //self.window =  UIWindow(frame: UIScreen.main.bounds)

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        guard let rootVC = storyboard.instantiateViewController(identifier: "RootTabBarC") as? RootTabBarC else {
            print("ViewController not found")
            return
        }
        let navVc = UINavigationController(rootViewController: rootVC)
        //navVc.isNavigationBarHidden = true
        self.window?.rootViewController = navVc
        self.window?.makeKeyAndVisible()
    }
    else
    {
        self.window = UIWindow(windowScene: windowScene)
        //self.window =  UIWindow(frame: UIScreen.main.bounds)

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        guard let rootVC = storyboard.instantiateViewController(identifier: "LoginSignUpVC") as? LoginSignUpVC else {
            print("ViewController not found")
            return
        }
        let navVc = UINavigationController(rootViewController: rootVC)
        //navVc.isNavigationBarHidden = true
        self.window?.rootViewController = navVc
        self.window?.makeKeyAndVisible()
    }



}

这是我的 LoginSignUpVc 代码

import UIKit

class LoginSignUpVC: UIViewController {

@IBOutlet weak var lblTitle: UILabel!
@IBOutlet weak var btnLogin: UIButton!
@IBOutlet weak var lblDontHaveAccount: UILabel!
@IBOutlet weak var btnSignUp: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()


    setupUI()
}

override func viewWillAppear(_ animated: Bool) {
    navigationController?.setNavigationBarHidden(false, animated: true)
}
override var prefersStatusBarHidden: Bool
{
    return true
}

func setupUI()
{
    letterSpacing()
    buttonSetUp()
}

@IBAction func onClickLogin(_ sender: Any)
{

}

@IBAction func onClickSignUp(_ sender: Any)
{

}

}

这是我的 LoginVc 代码

import UIKit
import FirebaseAuth

class LoginVC: UIViewController {

@IBOutlet weak var lblTitle: UILabel!
@IBOutlet weak var lblSubTitle: UILabel!
@IBOutlet weak var viewContainerEmail: UIView!
@IBOutlet weak var txtEmail: UITextField!
@IBOutlet weak var viewContainerPassword: UIView!
@IBOutlet weak var txtPassword: UITextField!
@IBOutlet weak var btnLogin: SSSpinnerButton!
@IBOutlet weak var btnForgotPassword: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.layer.frame.origin.y = 22
    setupUI()

}

override func viewWillDisappear(_ animated: Bool) {
    btnLogin.stopAnimate(complete: nil)
}

override var prefersStatusBarHidden: Bool
{
    return true
}

func setupUI()
{
    letterSpacing()
    textFieldSetUp()
    buttonSetUp()
}

@IBAction func onClickLogin(_ sender: Any)
{
    let email = Validation.shareValidator.isValidEmail(email: txtEmail.text, view: viewContainerEmail)
    let password = Validation.shareValidator.isValidPassword(password: txtPassword.text, view: viewContainerPassword)
    if email != "success"
    {
        CustomAlert.shareAlert.alertSetUp(title: "Email Field", subTitle: email, boldTitle: "Invalid Email")
    }
    else if password != "success"
    {
        CustomAlert.shareAlert.alertSetUp(title: "Password Field", subTitle: password, boldTitle: "Invalid Password")
    }
    else
    {
        Auth.auth().signIn(withEmail: txtEmail.text!, password: txtPassword.text!) { (result, error) in
            if error != nil
            {
                CustomAlert.shareAlert.alertSetUp(title: "Login Error", subTitle: error!.localizedDescription, boldTitle: "Login Error")
            }
            else
            {
                self.btnLogin.startAnimate(spinnerType: SpinnerType.circleStrokeSpin, spinnercolor: UIColor.white, spinnerSize: 20, complete: {
                    self.btnLogin.backgroundColor = UIColor.green

                    let defaults = UserDefaults.standard

                    defaults.set(true, forKey: "isLogin")
                    defaults.set(result?.user.uid, forKey: "uid")
                    defaults.set(self.txtEmail.text!, forKey: "email")
                    //let st = UIStoryboard(name: "Main", bundle: nil)
                    let vc = self.storyboard?.instantiateViewController(withIdentifier: "RootTabBarC") as! RootTabBarC
                    self.navigationController?.pushViewController(vc, animated: true)
                })
            }
        }

    }
}

@IBAction func onClickBack(_ sender: Any)
{
    navigationController?.popViewController(animated: true)
}

}

这是我的 ProfileVC 代码

import UIKit
import FirebaseAuth
import FirebaseDatabase
import Kingfisher

class ProfileVC: UIViewController {

@IBOutlet weak var viewImageContainer: UIView!
@IBOutlet weak var imgProfile: UIImageView!
@IBOutlet weak var lblName: UILabel!
@IBOutlet weak var lblDishExpert: UILabel!
@IBOutlet weak var lblBio: UILabel!
@IBOutlet weak var imgEmailIcon: UIImageView!
@IBOutlet weak var imgPhoneIcon: UIImageView!
@IBOutlet weak var lblEmail: UILabel!
@IBOutlet weak var lblPhone: UILabel!
var userDataDict = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    setUpUI()
    showUserData()
}

@IBAction func onClickLogOut(_ sender: Any)
{
    do
    {
        try Auth.auth().signOut()

        UserDefaults.standard.set(false, forKey: "isLogin")
        UserDefaults.standard.removeObject(forKey: "uid")
        UserDefaults.standard.removeObject(forKey: "email")
        UserDefaults.standard.synchronize()

    }
    catch let err
    {
        print(err.localizedDescription)
    }


    let vc = storyboard?.instantiateViewController(identifier: "LoginSignUpVC") as? LoginSignUpVC

    navigationController?.popToViewController(vc!, animated: true)

}



}

我也在尝试self.parent?.navigationController?.PopToRootViewController(animate:true) 但工作不正常。 感谢您的帮助。

最佳答案

当您第一次打开屏幕时,您正在设置 NavigationController 并根据您的登录功能添加登录或选项卡 Controller 。因此,如果我启动应用程序,它会将我带到 TabBar Controller,现在我按注销按钮进入 login Controller

但是导航堆栈中没有这样的 Controller 。所以我建议你再次调用场景方法来将登录 Controller 设置为rootController

您能做的最好的事情就是在场景类中为登录注销状态创建单独的功能。

func navigateToHomeVC(){
     self.window = UIWindow(windowScene: windowScene)
        //self.window =  UIWindow(frame: UIScreen.main.bounds)

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        guard let rootVC = storyboard.instantiateViewController(identifier: "RootTabBarC") as? RootTabBarC else {
            print("ViewController not found")
            return
        }
        let navVc = UINavigationController(rootViewController: rootVC)
        //navVc.isNavigationBarHidden = true
        self.window?.rootViewController = navVc
        self.window?.makeKeyAndVisible()
}


func navigateToLoginVC(){
      self.window = UIWindow(windowScene: windowScene)
        //self.window =  UIWindow(frame: UIScreen.main.bounds)

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        guard let rootVC = storyboard.instantiateViewController(identifier: "LoginSignUpVC") as? LoginSignUpVC else {
            print("ViewController not found")
            return
        }
        let navVc = UINavigationController(rootViewController: rootVC)
        //navVc.isNavigationBarHidden = true
        self.window?.rootViewController = navVc
        self.window?.makeKeyAndVisible()
}


func makeRoot()
{

    let defaults = UserDefaults.standard

    if defaults.bool(forKey: "isLogin") == true
    {
       navigateToHomeVC()

    }
    else{
        navigateToLoginVC()
    }
}

现在,当按下注销按钮时,调用场景类navigateToLoginVC()函数。

希望这有帮助。

关于ios - 尝试弹出到不存在的 View Controller swift 5.1 Xcode iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59796673/

相关文章:

ios - 如何在 Facebook 上发布签到信息?

ios - Xcode 中的 Getters 和 Setters 没有问题

在独立 View 上进行 iOS UI 测试

swift - 如何在 alamofire 中使用自定义动词?

iOS Mach-o 链接器错误解析

ios - 添加 subview 后,Swift Playground UITable View Cell 未调整大小

ios - AVCapture session 捕获图像 SWIFT

ios - 多点连接 手动连接/管理对等点

objective-c - 为什么Xcode中突然出现大量编译器错误?

ios - CLLocationManager 不在 viewDidLoad 上初始化