向所有 UIViewControllers 添加变量

add variable to all UIViewControllers

我是 Swift 的新手,我正在尝试在练习应用程序中实现自定义 UIKeyCommand 架构。我在下面为基础 UISplitViewController 编写了扩展,以在屏幕上的当前视图中显示所有 UIKeyCommands

extension UISplitViewController {
    open override var canBecomeFirstResponder: Bool {
        return true
    }

    var BPKeyCommands: [BPKeyCommand]? {
        var commands: [BPKeyCommand] = []

        var mastervc = self.viewControllers.first
        if (mastervc is UINavigationController) {
            mastervc = (mastervc as! UINavigationController).viewControllers.last
        }
        if let masterCommands = mastervc.commands {
            for command in masterCommands {
                commands.append(command)
            }
        }

        return commands
    }

    open override var keyCommands: [UIKeyCommand]? {
        var commands: [UIKeyCommand] = []

        if let bpkeycommands = BPKeyCommands {
            for command in bpkeycommands {
                let new = UIKeyCommand(input: command.input,
                                       modifierFlags: command.modifierFlags,
                                       action: #selector(executeKeyCommand(sender:)),
                                       discoverabilityTitle: command.title)
                commands.append(new)
            }
        }

        return commands
    }

    @objc private func executeKeyCommand(sender: UIKeyCommand) {
        if let index = keyCommands?.firstIndex(of: sender) {
            if let command = BPKeyCommands?[index] {
                command.action(command)
            }
        }
    }
}

现在,如您所料,这会在 if let masterCommands = mastervc.commands { 处引发错误,因为默认情况下 UIViewController doesn't contain the commands variable out of the box. My question is: how can I haveUIViewControllerhave that variable? Just like all controllers can overridekeyCommands`?

您可以创建 UIViewController 的扩展名并在 UIViewController 的扩展名上添加 属性。然后你会在子视图控制器上得到它,比如 UISplitViewController 或任何其他自定义 ViewControllers。要了解有关扩展程序的更多信息,Which can be added on extension or what can be done by extension??

您必须创建一个带有命令变量的协议并使您的视图控制器符合它(第 1 步)。您可以为特定视图控制器提供值,也可以提供默认实现。

第 1 步:- 使用您需要的变量创建协议。

protocol Commandable{
   var commands: [String]{set get}
}
extension Commandable{
   var commands: [String]{
        get{return ["hello","world"]}
        set{}
    }
}

第 2 步:- 使您正在使用的控制器符合它

第 3 步:- 更改上面的代码以获取命令

if let commandProtocol = masterVC as? Commandable
{
    let commands = commandProtocol.commands
}
else{
    // handle it
}

确保变量是唯一的,以免意外覆盖它。

谢谢。