ios - 从自定义 UIViewController 导航到 TabBarController 没有 segue

标签 ios swift uiviewcontroller uitabbarcontroller

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

这是一张带有错误的图片:

enter image description here

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

这是我的设计:

enter image description here

这是我的代码:

    // 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 现在是 admin 而不是 products 所以你需要在索引 0 处获取 VC,它是 products

productsInCartArray = ((self.tabBarController?.viewControllers![0] as! UINavigationController).viewControllers[0] as! ProductsViewController).selectedProductsArray

关于ios - 从自定义 UIViewController 导航到 TabBarController 没有 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51140937/

相关文章:

ios - 缩放整个 UICollectionView

ios - 通过点击 X 按钮识别 AVPlayerViewController 何时关闭

ios - UINavigationController 在设备旋转时自动弹出到 Root View

swift - 如何在 Swift 中取消本地通知触发器

swift - 选项默认为零吗?

ios - NSURL/NSURLRequest 意外地为零

ios - iOS swift 中 UIDocumentPickerController 的 Doc 文件问题?

xcode - 如何从 Storyboard 中制作的弹出窗口中获取变量值? (在 swift 中)

iphone - 如何在NavigationController上弹出UIViewController并同时推送UIView

ios - Swift iOS - 如何在两个 UIWindows 之间切换/切换并制作 keyWindow