我收到的错误似乎无法修复
由于未捕获的异常而终止应用 'NSInvalidArgumentException',原因:'无法找到故事板 在捆绑NSBundle中命名为'MainTabController'
应用程序将构建并显示登录屏幕,但在出现上述错误后立即崩溃。
我已尝试过与此类似的所有以下内容,并且没有运气。
问题似乎与我的 presentMainScreen() 方法有关
func presentMainScreen(){
let mainstoryboard = UIStoryboard(name: "MainTabController", bundle: nil)
let mainTabController = mainstoryboard.instantiateViewController(withIdentifier: "MainTabController") as! MainTabController
mainTabController.selectedViewController = mainTabController.viewControllers?[1]
//let storyboard:UIStoryboard = UIStoryboard(name:"Main", bundle:nil)
//let loggedInVC:LoggedInVC = storyboard.instantiateViewController(withIdentifier: "LoggedInVC") as! LoggedInVC
//self.present(loggedInVC, animated: true, completion: nil)
}
如果我注释掉mainTabController行,应用程序将完美运行,如果我取消注释loggedInVC行并且注释掉mainTabController行,它也可以完美地工作。
非常感谢任何建议。
下面是我的整个 ViewController.swift 代码和我工作区的屏幕截图
ViewController.swift
import UIKit
import FirebaseAuth
class ViewController: UIViewController {
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
if Auth.auth().currentUser != nil {
print("success")
self.presentMainScreen()
}
}
@IBAction func creatAccountTapped(_ sender: Any) {
if let email = emailTextField.text, let password = passwordTextField.text {
Auth.auth().createUser(withEmail: email, password: password, completion:{ user, error in
if let firebaseError = error {
print(firebaseError.localizedDescription)
return
}
print("success")
self.presentMainScreen()
})
}
}
@IBAction func loginButtonTapped(_ sender: Any) {
if let email = emailTextField.text, let password = passwordTextField.text {
Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
if let firebaseError = error {
print(firebaseError.localizedDescription)
return
}
print("success")
self.presentMainScreen()
})
}
}
func presentMainScreen(){
let mainstoryboard = UIStoryboard(name: "MainTabController", bundle: nil)
let mainTabController = mainstoryboard.instantiateViewController(withIdentifier: "MainTabController") as! MainTabController
mainTabController.selectedViewController = mainTabController.viewControllers?[1]
//let storyboard:UIStoryboard = UIStoryboard(name:"Main", bundle:nil)
//let loggedInVC:LoggedInVC = storyboard.instantiateViewController(withIdentifier: "LoggedInVC") as! LoggedInVC
//self.present(loggedInVC, animated: true, completion: nil)
}
}
答案 0 :(得分:4)
故事板的名称是什么?是MainTabController.storyboard
还是Main.storyboard
?
您正在尝试加载名为" MainTabController
"的故事板:
let mainstoryboard = UIStoryboard(name: "MainTabController", bundle: nil)
但之前你称之为Main.storyboard:
摆弄目标会员Main.storyboard。
此外,如果主标签控制器与登录视图控制器位于同一个故事板中,那么您可以尝试使用它:
let mainTabController = self.storyboard.instantiateViewController(withIdentifier: "MainTabController")
答案 1 :(得分:1)
您的故事板名为Main.storyboard
。 MainTabController
是Main
故事板中的控制器。
let mainstoryboard = UIStoryboard(name: "Main", bundle: nil)
let mainTabController = mainstoryboard.instantiateViewController(withIdentifier: "MainTabController") as! MainTabController
mainTabController.selectedViewController = mainTabController.viewControllers?[1]
答案 2 :(得分:0)
let mainstoryboard = UIStoryboard(name: "MainTabController", bundle: nil)
您的故事板名为“Main”