从自定义UIViewController导航到TabBarController的第二个选项卡(当然是另一个UIViewController)时,我遇到了问题。
如果我从AdminPanel导航到Products不会崩溃,因为我有一个从Products到AdminPanel的push。但是从AdminPanel到Cart我没有任何push

这是带有错误的图片:

ios - 从导航自定义UIViewController到TabBarController-LMLPHP

这是错误:“无法将类型'MyAppName.AdminPanelViewController'(0x10e878418)的值强制转换为'MyAppName.ProductsViewController'(0x10e877e00)。”

这是我的设计:

ios - 从导航自定义UIViewController到TabBarController-LMLPHP

这是我的代码:

    // 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

07-26 09:43