我应该按照哪种方式设置系统ID

时间:2018-01-26 12:12:08

标签: ios swift

我有一个名为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
}

0 个答案:

没有答案