我有一个名为newSystems的全局数组,它包含以下System类的对象:
class System: NSObject, NSCoding {
var systemView: UIViewController?
private let _systemName: String!
private let _systemType: String!
let _systemImplForCustomerID: String!
init(name: String, type: String, systemID: String) {
_systemName = name
_systemType = type
_systemImplForCustomerID = systemID
systemView = System.setSystemView(_systemType: type)
}
private static func setSystemView(_systemType: String) -> UIViewController{
var sysView: UIViewController!
switch _systemType {
case "Room":
sysView = mainStoryboard.instantiateViewController(withIdentifier: "View1") as! UINavigationController
case "Kitchen":
sysView = mainStoryboard.instantiateViewController(withIdentifier: "View2") as! KitchenVC
default:
break;
}
return sysView
}
}
现在在TabBarController
我将systemView
属性分配给viewControllers
数组:
for system in newSystems {
if let newView = system.systemView {
self.viewControllers?.insert(newView, at: 0)
}
}
现在TabBarController
显示tabBar
中的2个项目,两者都可以访问并正常运行。
但现在在房间里我想使用_systemImplForCustomerID
RoomVC的例子:
class RoomVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = UICollectionViewCell()
*** Here I need the _systemImplForCustomerID ***
return cell
}
我的问题是如何获得_systemImplForCustomerID
财产?或者我应该遵循哪种方式来实现它。作为临时解决方案,我创建了过滤全局数组newSystems
并且工作正常的函数,但它是正确的解决方案吗?
func findCustomerSystemImplID(view: UIViewController) -> String? {
var id: String?
let currentSystem = newSystems { $0.systemView?.view.accessibilityIdentifier == view.view.accessibilityIdentifier }
if let sysID = currentSystem.first?.customerSystemImplID {
id = sysID
}
return id
}