在通过协议传递数据时我做错了什么

问题描述 投票:0回答:3

我试图在viewControllers之间传递数据,但似乎有些不对劲。

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

这就是我这样做的方式:

//
//  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
    }
}
ios swift protocols
3个回答
0
投票

而不是在PickerNodeDelegate = self闭合中设置didSet {}

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

而是在init()函数中分配它

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

请注意,您应该将您的委托声明为weak,因为它是委托,您的协议应该符合类类型才能被削弱。

protocol ComboBoxNodeDelegate: class
...
weak var delegate: ComboBoxNodeDelegate?

0
投票

这是一个例子,希望它有所帮助!

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!"
    } 
}

-1
投票

在Swift中传递的所有参数都是常量 - 因此您无法更改它们。

如果要在函数中更改它们,则必须声明您的协议通过inout引用传递:

protocol ComboBoxNodeDelegate {
    func getCustomOption(data: inout Bool)
}

注意:您不能将常量(let)传递给此函数。它必须是一个变量 - 我看到你正在做的!

© www.soinside.com 2019 - 2024. All rights reserved.