ios - 从自定义 UIViewController 导航到 TabBarController 而不使用 segue
问题描述
当我从自定义 UIViewController 导航到 TabBarController 的第二个选项卡时遇到问题,这当然是另一个 UIViewController。如果我从 AdminPanel 导航到 Products 不会崩溃,因为我有一个push
从 Products 到 AdminPanel。但是从 AdminPanel 到 Cart 我没有任何push
.
这是带有错误的图片:
这是错误:“无法将'MyAppName.AdminPanelViewController'(0x10e878418)类型的值转换为'MyAppName.ProductsViewController'(0x10e877e00)。”
这是我的设计:
这是我的代码:
// PRODUCTS VC
class ProductsViewController: UIViewController{
var selectedProductsArray = [Product]()
var priceForSelectedProductsArray = [Float]()
// Func which is using GestureRecognition to access the Admin Panel when we press on User Avatar
func accessToAdminPanel(){
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ProductsViewController.adminImageTapped(gesture:)))
userAvatarImageView.addGestureRecognizer(tapGesture)
}
// Function to open the AdminPanelViewController
@objc func adminImageTapped(gesture: UIGestureRecognizer) {
let storyBoard : UIStoryboard = UIStoryboard(name: Constants.nameOfMainSB, bundle:nil)
let adminPanelVC = storyBoard.instantiateViewController(withIdentifier: Constants.adminPanelStoryboard) as! AdminPanelViewController
self.navigationController?.pushViewController(adminPanelVC, animated: true)
}
// Func which will add the product into an array of products when the user press Add To Cart
func didTapAddToCart(_ cell: ProductTableViewCell) {
let indexPath = self.productsTableView.indexPath(for: cell)
addProduct(at: indexPath!)
selectedProductsArray.append(productsArray[(indexPath?.row)!]) // Append products for cart
priceForSelectedProductsArray.append(productsArray[(indexPath?.row)!].price) // Append prices for selected products
}
}
// CART VC
class CartViewController: UIViewController {
var productsInCartArray = [Product]()
var productPricesArray = [Float]()
// Append the selectedProducts into productsInCartArray using the TabBarController
func fetchSelectedProducts() {
// ------------------HERE IS CRASHING AT THIS LINE -----------------------
productsInCartArray = ((self.tabBarController?.viewControllers![0] as! UINavigationController).topViewController as! ProductsViewController).selectedProductsArray
productPricesArray = ((self.tabBarController?.viewControllers![0] as! UINavigationController).topViewController as! ProductsViewController).priceForSelectedProductsArray
totalSum = productPricesArray.reduce(0, +)
}
}
如果您无法通过这部分代码找出崩溃的地方,我可以给您指向 git 源代码的链接,因为我没有什么要隐藏的。
如果您正在阅读本文,非常感谢您抽出宝贵的时间,我希望您能帮助我,因为半天以来,我试图找到一个解决方案来修复这个导致我的应用程序崩溃的错误。
解决方案
topViewController 现在是管理员而不是产品,因此您需要在索引 0 处获取 VC,即产品
productsInCartArray = ((self.tabBarController?.viewControllers![0] as! UINavigationController).viewControllers[0] as! ProductsViewController).selectedProductsArray
推荐阅读
- javascript - svg 没有正确旋转
- python - 如何从 COM 客户端获取 Excel 单元格的值
- visual-studio - 如何使用 Visual Studio 2017 升级 NuGet?
- javascript - GraphQL.js - 时间戳标量类型?
- yocto - 运行 openocd 的 bitbake 任务
- linux - '-mmcu=' 后缺少设备或架构
- c++ - c++ doubles and floats,如何绕过丑陋的真相
- python - 使用 PyMongo 和 x509 SSL 证书连接到 MongoDB 数据库时出现 ServerSelectionTimeoutError
- python - Python Requests 或 Urllib - 使用其中任何一个来填写 Web 表单?
- c# - 错误“参数是值,而参数声明为”是什么意思?