简体   繁体   English

iOS-如何使用initWithViewController定位“父” viewcontroller

[英]iOS - how to target the “parent” viewcontroller with initWithViewController

I would like to use a method in a viewcontroller that is the "container" of an other viewcontroller but not directly the parent. 我想在一个视图控制器中使用一个方法,该方法是另一个视图控制器的“容器”,而不是直接父对象。 I display a popover containing a custom xib file. 我显示一个包含自定义Xib文件的弹出窗口。 For that I have three viewcontrollers: 为此,我有三个视图控制器:

1) popoverVC managing the settings of the popover (like alpha value, any arrow, size, and the method I want to use from "outside" dismisspopover) 1) popoverVC管理弹出式窗口的设置(例如alpha值,任何箭头,大小和我要从“外部”使用的方法dismisspopover)

2) customVC with a xib file to change the content of my popover (some text and some buttons). 2)customVC与厦门国际银行文件,以改变我酥料饼的内容(一些文字和一些按钮)。

3) mainVC from where the popover is launched and where I receive some actions when interacting with my buttons in customVC. 3) mainVC从哪里启动弹出窗口,并且在与customVC中的按钮进行交互时收到一些操作。 mainVC is the delegate of customVC. mainVC是customVC的委托。

My problem is I don't know of to call dismisspopover method in popoverVC from an action in customVC. 我的问题是我不知道从customVC中的操作调用popoverVC中的dismisspopover方法。 At the very beginning I initialize my customVC like this from mainVC: 首先,我从mainVC初始化我的customVC:

mainVC.m mainVC.m

    customVC *cvc = [[customVC alloc] init];
    [cvc setDelegate:self];

    PopoverController *popover = [[PopoverController alloc] initWithViewController:cvc];
    [popover presentPopoverFromView:textField];

I tried to instantiate a new instance of popoverVC inside my action method in customVC to be able to call the method [popoverdismiss]. 我试图在customVC的action方法中实例化popoverVC的新实例,以便能够调用方法[popoverdismiss]。 But this should not be working since I will be targeting a different instance from the one I started with... And I would like to be able to target popoverVC like [self.presentingVC] but I am not sure the method initWithVC sets a hierarchy like this. 但这不应该起作用,因为我将针对与开始的实例不同的实例...而且我希望能够像[self.presentingVC]这样定位popoverVC,但是我不确定initWithVC方法是否设置了层次结构像这样。

Does anyone has an idea? 有人有主意吗?

The way I usually go about having to circumvent the default view controller hierarchy is to create a property within your child view controller (I think customVC ) called parent or something to that effect that's of type mainVC . 我通常必须避开默认视图控制器层次结构的方法是在您的子视图控制器(我认为customVC )中创建一个称为parent的属性或具有mainVC类型的mainVC Then either create a new initializer for your customVC that incorporates a field to assign to parent or just assign to it after you first initialize your customVC . 然后为您的customVC创建一个新的初始化器,该初始化器并入一个字段以分配给parent或者在您首次初始化customVC之后才分配给它。 Then whenever your customVC needs to ask the mainVC to perform a function, you can make a method call from parent . 然后,每当customVC需要要求mainVC执行功能时,您都可以从parent进行方法调用。

Alternatively, you could just as easily make parent correspond to your popoverVC object, I'm not totally sure which object you need to access, but it should be as simple as creating your own property for it. 另外,您也可以很容易地使parent对象对应于您的popoverVC对象,但我不能完全确定您需要访问哪个对象,但是它应该与为其创建自己的属性一样简单。

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

相关问题 iOS 当呈现的视图控制器被取消时在父视图控制器中得到通知 - iOS get notified in parent viewcontroller when presented viewcontroller is dimissed iOS更改父ViewController的Integer / NSNumber - iOS change Integer/NSNumber of parent ViewController iOS容器视图和父ViewController通信? - iOS Container View and Parent ViewController communication? 如何在 ios 中关闭视图控制器? - How to dismiss viewcontroller in ios? 如何刷新父viewcontroller的viewdidload - how to refresh the viewdidload for parent viewcontroller Swift:在公共功能中使用“自我”定位父ViewController吗? - Swift: using “self” to target parent ViewController in public functions? 如何使用目标C调用父ViewController到子Viewcontroller方法? - How To Call Parent Viewcontroller To Child Viewcontroller Method Using Objective C? 如何正确解散呈现的ViewController并返回到父ViewController? - How to properly dismiss the presented ViewController and go back to the parent ViewController? 如何将数据从模态呈现的 ViewController 传递到 swift 5 中的父 ViewController? - How to pass the data from modally presented ViewController to parent ViewController in swift 5? 关闭模态视图控制器后如何更新父视图控制器的视图 - How to update View of parent Viewcontroller after a modal Viewcontroller is dimissed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM