gpt4 book ai didi

ios - 试图弹出一个不存在的 View Controller swift 5.1 Xcode iOS

转载 作者:行者123 更新时间:2023-12-01 22:05:42 26 4
gpt4 key购买 nike

我正在开发应用程序。当我单击注销按钮时,它向我显示此错误 试图弹出一个不存在的 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并添加 login or tab Controller根据您的登录功能。因此,如果说我启动了应用程序,它会将我带到 TabBar Controller现在我按下注销按钮去 login Controller .

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

您能做的最好的事情是在 scene class 中创建单独的功能为 logged inlogged out状态。

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() scene class 的功能当按下注销按钮时。

希望这可以帮助。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com