您好,我有一个通用链接可以在我的应用中实现。我创建了一个通用函数来实现这一点,代码如下
fileprivate func gotoVC<T>(viewcontroller:T,storyBoard: UIStoryboard) {
let vc = storyBoard.instantiateViewController(withIdentifier: "\(T.self)")
self.navigationController?.pushViewController(vc, animated: true)
}
这很完美,但对于某些 View Controller ,我必须传递 ID(例如:调用 API 的 ID,如产品 ID)
任何人都可以帮助我使用这种方法将这些 ID 发送到 View Controller 吗???
最佳答案
一种方法是声明一个具有apiId
属性的协议(protocol)(让我们称之为ApiIdHolder
),然后让您的目标 View Controller 实现该协议(protocol)。现在,将以下行添加到您的 gotoVC()
实现中,紧接在 instantiate(withIdentifier:)
调用之后:
(vc as? ApiIdHolder)?.apiId = apiId
或者,不那么简洁,
if let apiIdHolder = vc as? ApiIdHolder {
apiIdHolder.apiId = apiId
}
this 在通用函数中的事实无关紧要;它适用于任何功能。
关于iOS Swift 通用函数 tot 处理通用链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50832988/