[英]Get the presenting viewController of viewController.view in swift
i am trying to get the presenting viewController of a viewController's view The idea is like that : i have a我正在尝试获取 viewController 视图的呈现 viewController 这个想法是这样的:我有一个
viewController = CategoriesViewController
and i am presenting its view inside anther我在花药中展示它的观点
viewController = HomeViewController
by using通过使用
CategoriesViewController.view
so when i want to reach the CategoriesViewController.view
所以当我想到达
HomeViewController
from CategoriesViewController
来自CategoriesViewController
HomeViewController
i do this我这样做
let vc = self.presentingViewController as? HomeViewController
but it is telling me that it is nil i tried the但它告诉我它是零我试过
.parentViewController
and it is returning它正在返回
CategoriesViewController
In case you want to change a variable in HomeVieController
using CategoriesViewController
you could create your own protocol .如果您想使用CategoriesViewController
更改HomeVieController
的变量,您可以创建自己的 协议。 You can use protocols to communicate between different controllers.您可以使用协议在不同的控制器之间进行通信。
protocol ChangeVariableProtocol {
func changeVar(variable: Int)
}
In the protocol itself you only declare methods.在协议本身中,您只声明方法。 In your CategoriesViewController
you would create a delegate Varibale like this在您的CategoriesViewController
您将创建一个像这样的委托变量
var changeVarDelegate: ChangeVariableProtocl?
Whenever you want to change the variable in CategoriesViewController
you call your protocol method.每当您想更改CategoriesViewController
的变量时,您都会调用您的协议方法。
changeVarDelegate?.changeVar(10)
In HomeViewController you need to implement this protocol and initialize the changeVarDelegate variable.在 HomeViewController 中,您需要实现此协议并初始化 changeVarDelegate 变量。
extension HomeViewController: ChangeVarProtocol {
func changeVar(var: variable) {
// Implement your own logic here
self.valueToChange = variable
}
And make sure that you initialize changeVarDelegate
when you are instancing your CategoriesViewController
.并确保在实例化CategoriesViewController
时初始化changeVarDelegate
。
Hope this helps!希望这可以帮助!
CategoriesViewController
and in its view.在CategoriesViewController
及其视图中创建一个回调闭包属性。viewDidLoad
hand the closure over to the view.在viewDidLoad
将闭包交给视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.