简体   繁体   English

Swift - 从另一个类访问数组

[英]Swift - access array from another class

In my project I have an array where the user can append elements.在我的项目中,我有一个array ,用户可以在其中添加元素。 My problem right now is that I have another class with a tableView where I want to display all the elements dynamically so if the user adds and element to the array it is also in my tableView .我现在的问题是我有另一个带有tableView class ,我想在其中动态显示所有elements因此如果用户将element添加到array它也在我的tableView

Class A A级

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

Class B B级

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

Update更新

So thanks for the comments so far, I got the main problem I had but I am still struggling.所以感谢到目前为止的评论,我得到了我遇到的主要问题,但我仍在挣扎。

My setup:我的设置:

I create my dropDownButton (which contain a dropDownView ) in my ViewController-Class where I also have my var wishListTitlesArray .我在我的ViewController-Class中创建了我的dropDownButton (其中包含一个dropDownView ),我也有我的var wishListTitlesArray

I tried dropDownButton.dropView.dropDownOptions = wishListTitlesArray .我试过dropDownButton.dropView.dropDownOptions = wishListTitlesArray However that does not do the full job.然而,这并不能完成全部工作。

@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)

This is where the user can add an element to the wishListTitlesArray :这是用户可以向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)

        })

and the problem right now is that if the user adds an element, dropDownOptions is not updating the data accordingly.现在的问题是,如果用户添加一个元素, dropDownOptions不会相应地更新数据。 So how can I access the updated list?那么如何访问更新后的列表呢? Thanks for all the comments so far!感谢您到目前为止的所有评论!

This is fairly straightforward OO programming.这是相当简单的面向对象编程。 As somebody pointed out, you're dealing with instance variables and instances of classes, not classes.正如有人指出的那样,您正在处理实例变量和类的实例,而不是类。

An analogy:一个比喻:

Instances: If you want one instance of a class to get a value from an instance of another class, it's like having your Toyota car request the radio station presets from your friend's Honda and set your radio the same way.实例:如果您希望一个类的一个实例从另一个类的实例中获取一个值,这就像让您的丰田汽车从您朋友的本田请求电台预设并以同样的方式设置您的收音机。

Classes: The Toyota motor company decides they like the radio presets that Honda uses, so they read the radio station presets from the Honda company and the Toyota factory uses those settings for all new Toyotas.课程:丰田汽车公司决定他们喜欢本田使用的无线电预设,因此他们阅读本田公司的无线电台预设,而丰田工厂将这些设置用于所有新丰田汽车。

In class A, make wishListTitlesArray public:在 A 类中,将wishListTitlesArray public:

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

Then, your Class B object will need a pointer to the Class A object.然后,您的 B 类对象将需要一个指向 A 类对象的指针。 There are various ways to do that.有多种方法可以做到这一点。 Let's say you set up Class B to take a Class A object at init time:假设您设置 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