首页 > 解决方案 > 通过协议传递数据我做错了什么

问题描述

我正在尝试在 viewControllers 之间传递数据,但似乎有问题。

我想将“Bool”设置为协议函数的第一个 viewController 以便能够在另一个屏幕中恢复。我做错了什么,我总是使用协议,但此时我遇到了麻烦。

我就是这样做的:

//
//  ComboBoxNode.swift
//

import Foundation
import SWXMLHash


protocol ComboBoxNodeDelegate {
    func getCustomOption(data:Bool)
}

class ComboBoxNode: FormControlNode, IFormControlDataSource {


    var listType: String?
    var dataSource: String?
    var dataSourceValue: String?
    var dataSourceText: String?
    var hasCustomOption:Bool?
    var customOptionText: String?
    var ctrlDataSourceType: String?
    var parameters = [ParameterNode]()
    var staticList: FormControlStaticListNode?

    var delegate:ComboBoxNodeDelegate?

    override init(indexer: XMLIndexer) {
        super.init(indexer: indexer)

        guard let element = indexer.element else {
            preconditionFailure("Error")
        }


        let isCustomOption = element.bool(by: .hasCustomOption) ?? hasCustomOption

        if isCustomOption == true {
            self.delegate?.getCustomOption(data: hasCustomOption!)
        }


        self.readFormControlDataSource(indexer: indexer)
    }

    override func accept<T, E: IViewVisitor>(visitor: E) -> T where E.T == T {
        return visitor.visit(node: self)
    }
}

这就是我试图在下一个屏幕上恢复的方式:

//  FormPickerViewDelegate.swift

import Foundation
import ViewLib
import RxSwift

class FormPickerViewDelegate: NSObject {

    var items = Variable([(value: AnyHashable, text: String)]()) {
        didSet {
            PickerNodeDelegate = self
            self.setDefaultValues()
        }
    }

    private var controlViewModel: FormControlViewModel
    private var customText:Bool?

    private var PickerNodeDelegate:ComboBoxNodeDelegate?

    init(controlViewModel: FormControlViewModel) {
        self.controlViewModel = controlViewModel
    }

    func getItemByValue(_ value: Any) -> (AnyHashable, String)? {

        if value is AnyHashable {
            let found = items.value.filter {$0.value == value as! AnyHashable}
            if found.count >= 1 {
                return found[0]
            }
        }

        return nil
    }
}


extension FormPickerViewDelegate:ComboBoxNodeDelegate {
    func getCustomOption(data: Bool) {
        customText = data
    }
}

标签: iosswiftprotocols

解决方案


举个例子,希望对你有帮助!

protocol ComboBoxNodeDelegate {
    func getCustomOption(data:Bool) -> String 
}

class ViewOne:ComboBoxNodeDelegate {
    var foo:Bool = false
    var bar:String = "it works!"

    /** Return: String */
    func getCustomOption(data:Bool) -> String { //conform here to protocol
          // do whatever you wanna do here ...example
          self.foo = data // you can set
          return bar // even return what you want

    }

    //initialize
    func initalizeViewTwo() {
         let v2 = ViewTwo()
         v2.delegate = self //since `self` conforms to the ComboBoxNodeDelegate protcol you are allowed to set
    }


}

class ViewTwo { 
    var delegate:ComboBoxNodeDelegate?

    func getCustomOption_forV1() {
        let view2_foo = delegate.getCustomOption(data:true)
        print(view2_foo) // should print "it works!"
    } 
}

推荐阅读