简体   繁体   English

在 swift 中获取 viewController.view 的呈现视图控制器

[英]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!希望这可以帮助!

  • Create a callback closure property both in CategoriesViewController and in its view.CategoriesViewController及其视图中创建一个回调闭包属性。
  • Right before presenting the controller assign the closure to change the value of a property to the callback property in the controller.在呈现控制器之前分配闭包以将属性的值更改为控制器中的回调属性。
  • In viewDidLoad hand the closure over to the view.viewDidLoad将闭包交给视图。
  • Call the closure in the view to be executed in the presenting controller.在呈现控制器中调用要执行的视图中的闭包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用NavigationViewController swift呈现ViewController - presenting ViewController with NavigationViewController swift 在UITabBarController中的ViewController中呈现视图 - Presenting a view in a ViewController that is in a UITabBarController iOS ZXingWidget - 在自己的 ViewController.view 中使用 ZXingWidgetViewController 的视图作为子视图 - iOS ZXingWidget - use view of ZXingWidgetViewController in own ViewController.view as subview Swift iOS -NSMutableAttributedString不呈现ViewController吗? - Swift iOS -NSMutableAttributedString Not Presenting ViewController? Swift:通过UITabBarController呈现一个viewController - Swift: Presenting a viewController over UITabBarController 在 swift 中呈现自定义 ViewController class? - Presenting custom ViewController class in swift? viewController.view没有显示但在ViewDebugMode时出现 - viewController.view does not showing but it appears when ViewDebugMode [window addSubview:viewcontroller.view]中的EXC_BAD_ACCESS - EXC_BAD_ACCESS in [window addSubview:viewcontroller.view] 可以将同一ViewController.view作为子视图作为Singleton添加到不同的视图吗? - Can the same ViewController.view be added as subview to different views as a Singleton? Swift - 再次呈现 ViewController 后,推动 ViewController 不起作用 - Swift - pushing ViewController not working after presenting ViewController again
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM