繁体   English   中英

Swift - 从另一个类访问数组

[英]Swift - access array from another class

在我的项目中,我有一个array ,用户可以在其中添加元素。 我现在的问题是我有另一个带有tableView class ,我想在其中动态显示所有elements因此如果用户将element添加到array它也在我的tableView

A级

var wishListTitlesArray: [String] = [String]()

B级

var dropDownOptions = [String]() // TableView data -> here I would like to access `wishListTitlesArray`

更新

所以感谢到目前为止的评论,我得到了我遇到的主要问题,但我仍在挣扎。

我的设置:

我在我的ViewController-Class中创建了我的dropDownButton (其中包含一个dropDownView ),我也有我的var wishListTitlesArray

我试过dropDownButton.dropView.dropDownOptions = wishListTitlesArray 然而,这并不能完成全部工作。

@objc func addWishButtonTapped(notification : Notification){

    popUpView.popUpTextField.text = ""
    self.popUpView.popUpTextField.becomeFirstResponder()

    view.addSubview(visualEffectView)
    view.addSubview(popUpView)
    view.addSubview(wishButton)
    self.view.addSubview(dropDownButton)


    // constrain blurrEffectView
    visualEffectView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    visualEffectView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    visualEffectView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    visualEffectView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

    // constrain popUpView
    popUpView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    popUpView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -50).isActive = true
    popUpView.heightAnchor.constraint(equalToConstant: 230).isActive = true
    popUpView.widthAnchor.constraint(equalToConstant: view.frame.width - 85).isActive = true

    // constrain wishButton
    wishButton.centerXAnchor.constraint(equalTo: popUpView.centerXAnchor).isActive = true
    wishButton.centerYAnchor.constraint(equalTo: popUpView.centerYAnchor, constant: 70).isActive = true
    wishButton.heightAnchor.constraint(equalToConstant: 72).isActive = true
    wishButton.widthAnchor.constraint(equalToConstant: 72).isActive = true

    // constrain DropDownButton
    dropDownButton.centerXAnchor.constraint(equalTo: self.popUpView.centerXAnchor).isActive = true
    dropDownButton.centerYAnchor.constraint(equalTo: self.popUpView.centerYAnchor).isActive = true
    dropDownButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
    dropDownButton.heightAnchor.constraint(equalToConstant: 40).isActive = true

    // set the drop down menu's options
    dropDownButton.dropView.dropDownOptions = wishListTitlesArray



    self.view.bringSubviewToFront(visualEffectView)
    self.view.bringSubviewToFront(popUpView)
    self.view.bringSubviewToFront(wishButton)
    self.view.bringSubviewToFront(dropDownButton)
    self.view.bringSubviewToFront(dropDownButton.dropView)

这是用户可以向wishListTitlesArray添加元素的wishListTitlesArray

if let txt = listNameTextfield.text {

        self.newListTextfield.resignFirstResponder()

        // append user-entered text to the data array
        self.wishListTitlesArray.append(txt)
        self.wishListImagesArray.append(self.image!)

        // DonMag3 - append new empty wish array
        self.userWishListData.append([Wish]())

        let theCustomWishlistView = createCustomWishlistView()

        self.view.addSubview(theCustomWishlistView)
        // constrain CustomWishlistView
        theCustomWishlistView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 120.0).isActive = true
        theCustomWishlistView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
        theCustomWishlistView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 30.0).isActive = true
        theCustomWishlistView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -30.0).isActive = true
        theCustomWishlistView.wishlistImage.image = self.image
        theCustomWishlistView.wishlistLabel.text = txt
        theCustomWishlistView.transform = CGAffineTransform(translationX: 0, y: 1000)

        self.view.bringSubviewToFront(containerView)

        // reload the collection view
        theCollectionView.reloadData()
        theCollectionView.performBatchUpdates(nil, completion: {
            (result) in
            // scroll to make newly added row visible (if needed)
            let i = self.theCollectionView.numberOfItems(inSection: 0) - 1
            let idx = IndexPath(item: i, section: 0)
            self.theCollectionView.scrollToItem(at: idx, at: .bottom, animated: true)

            // close (hide) the "New List" view
            self.closeButtonTappedNewList(nil)

        })

现在的问题是,如果用户添加一个元素, dropDownOptions不会相应地更新数据。 那么如何访问更新后的列表呢? 感谢您到目前为止的所有评论!

这是相当简单的面向对象编程。 正如有人指出的那样,您正在处理实例变量和类的实例,而不是类。

一个比喻:

实例:如果您希望一个类的一个实例从另一个类的实例中获取一个值,这就像让您的丰田汽车从您朋友的本田请求电台预设并以同样的方式设置您的收音机。

课程:丰田汽车公司决定他们喜欢本田使用的无线电预设,因此他们阅读本田公司的无线电台预设,而丰田工厂将这些设置用于所有新丰田汽车。

在 A 类中,将wishListTitlesArray public:

public var wishListTitlesArray: [String] = [String]()

然后,您的 B 类对象将需要一个指向 A 类对象的指针。 有多种方法可以做到这一点。 假设您设置 B 类以在初始化时获取 A 类对象:

Class B {
   var myAObject: A
   var dropDownOptions: [String]

   init(aObject: A) {
      myAObject = anObject
      dropDownOptions = myAObject.wishListTitlesArray 
   }
//more code here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM