ios - Swift - 从另一个类访问数组
问题描述
在我的项目中,我有一个array
用户可以附加元素的地方。我现在的问题是我有另一个我想动态显示所有内容的地方,class
所以如果用户添加并且它也在我的.tableView
elements
element
array
tableView
A级
var wishListTitlesArray: [String] = [String]()
B类
var dropDownOptions = [String]() // TableView data -> here I would like to access `wishListTitlesArray`
更新
因此,感谢到目前为止的评论,我遇到了主要问题,但我仍在苦苦挣扎。
我的设置:
我在我也有我的地方创建了我的dropDownButton
(其中包含一个dropDownView
)。ViewController-Class
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
:
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 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
}
推荐阅读
- python-3.x - 图像分类器ValueError:检查目标时出错:预期dense_31有2维,但得到的数组形状为(1463、224、224、3)
- joomla - 带有照片和重定向的 joomla 问题
- python - Python/Matplotlib - 为同一图形上的多个散点图设置颜色栏
- css - 避免菜单 li 底部阴影 css
- reactjs - 如何通过 Web 套接字发送实时视频的图像并将其存储在 React 状态变量中?
- python - 具有来自目录的流的多输入模型
- javascript - 谷歌驱动器总是通过 API 导出空白 pdf
- database - 如何使用 NoSQL 按特定日期范围内的投票对帖子进行排序?
- c# - C# 泛型类的字典
- vba - 如何使用 API 在 SolidWorks 中获取对象的坐标数据