本文介绍了试图弹出一个不存在的视图控制器 swift 5.1 Xcode iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发应用程序.当我点击注销按钮时,它向我显示了这个错误试图弹出一个不存在的视图控制器.

i'm Working on App. when i'm clicking on logout button its shows me this error Tried to pop to a view controller that doesn't exist.

这是我的 StoryBoard 屏幕

这是我的 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)但不能正常工作.感谢您的帮助.

i'm also try self.parent?.navigationController?.PopToRootViewController(animate:true)but is not working propely.thank for your help.

推荐答案

Vasucd 是对的,先把 LoginSinUpVC 放在 NavigationController 的 viewControllers 中

Vasucd is right, put LoginSinUpVC in NavigationController’s viewControllers first

——————编辑——————-

——————Edited—————-

如错误所示,这是因为您初始化了一个在 NavigationController 的 viewControllers 中不存在的新 viewController

As error indicates, this is because you init a new viewController that dose not exist in NavigationController's viewControllers

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

解决问题,尝试在navigationController的viewControllers中找到LoginSignUpVC

to solve the problem, try to find LoginSignUpVC in navigationController's viewControllers

for vc in navigationController.viewControllers {
    if let loginSignUpVC  = vc as? LoginSignUpVC {
        navigationController?.popToViewController(loginSignUpVC, animated: true)
    }
}

这篇关于试图弹出一个不存在的视图控制器 swift 5.1 Xcode iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 13:31