在iOS中使用协议传递多个值的正确方法

时间:2017-08-17 11:47:07

标签: ios swift protocols

所以我有两个ViewControllers。第一个(MapVC)带有地图,第二个(SettingsVC)有许多需要应用于此地图的设置。

我认为创建像

这样的协议会很棒
protocol MapSettingsDelegate: class {}

我知道我可以在此协议中指定功能。但是当我有很多设置时我应该怎么做 - 我应该如何将它们从SettingsVC传递给MapVC。

1 个答案:

答案 0 :(得分:5)

示例:

struct MySettings {
    var value1: String
    var value2: String
    // and so on...
}

protocol MapSettingsDelegate: class {
    func settingsUpdated(newSettings: MySettings)
}

并在你的控制器中实现它

class MapVC : MapSettingsDelegate {
    ...
    func settingsUpdated(newSettings: MySettings) {
        // Update everything you need
    }
    ...
}

随时询问详情