简体   繁体   English

从模态(objective-c)引用视图控制器

[英]Reference presenting view controller from modal (objective-c)

I have a navigation stack consisting of: 我有一个导航堆栈包括:

UINavigationViewController
 -> WelcomeViewController
 -> RoleViewController

I would like to add a modal viewcontroller in between Welcome and Role. 我想在Welcome和Role之间添加一个模态viewcontroller。 I add a ModalViewController via storyboard and assign a segue (Present Modally) to the ModalViewController from a button in WelcomeViewController. 我通过故事板添加一个ModalViewController,并从WelcomeViewController中的一个按钮为ModalViewController分配一个segue(Present Modally)。 It's all good and the modal shows up fine. 一切都很好,模态很好。

Now that I'm inside the ModalViewController I would like to dismiss the modal and perform the original segue from Welcome to Role. 现在我在ModalViewController里面, 我想解除模态并从Welcome to Role 执行原始的segue I did some searching on StackOverflow and came over different takes on how to do this. 我在StackOverflow上做了一些搜索,并找到了如何做到这一点的不同看法。 The most logical way was set out here: Dismissviewcontroller and perform segue 这里列出了最合乎逻辑的方法: Dismissviewcontroller并执行segue

UIViewController *parentController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^(void){
    [parentController performSegueWithIdentifier:@"segue_gotoRole" sender:self];
}];    

The modal is dismissed but the segue is not executed and the app crashes: 模态被解雇但是segue没有被执行而应用程序崩溃了:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'Receiver (<NavigationViewController: 0x78b96b80>) has no segue
with identifier 'segueToRole''

So the self.presentingViewController targets the UINavigationViewController and not the view controller WelcomeViewController which is actually presenting the modal. 所以self.presentingViewController的目标是UINavigationViewController,而不是实际呈现模态的视图控制器WelcomeViewController。 To succeed I need to find a way to get the presenting view controller which is defined in the storyboard as the segue originator from within the modal . 为了成功,我需要找到一种方法来获取在故事板中定义的呈现视图控制器作为模式内的segue创建者

I've tried a lot of different combinations, eg self.parentViewController, self.presentationViewController, I've even tried to loop through the navigation stack to single out the WelcomeViewController and target it specifically as the object to perform the segue, but nothing seems to work. 我已经尝试了很多不同的组合,例如self.parentViewController,self.presentationViewController,我甚至试图遍历导航堆栈以单独输出WelcomeViewController并将其专门定位为执行segue的对象,但似乎没什么上班。

Please help me. 请帮我。 :) :)

如果是我,我会写一个delegate protocol在这里Modal VC ,你提出并WelcomeViewControllerdelegate -一旦它被驳回,则WelcomeViewController可以接管和原因请看RoleViewController

What I did is to send the presenter reference to the presented instance. 我所做的是将演示者引用发送给呈现的实例。

This can be implemented by setting up a property of presenting class reference in the presented class. 这可以通过在所呈现的类中设置呈现类引用的属性来实现。

For example, 例如,

@property (strong, nonatomic) PresentingClass *presenter;

.

Then, in the presenting class implementation 然后,在呈现类实现中

presentedClass.presenter = self;
[self presentViewController:presentedClass animated:YES completion:nil];

Finally, in the presented implementation, simply call 最后,在提出的实现中,只需调用即可

if(presenter){
  [presenter foo];
}

Could help. 有帮助。 Let me know. 让我知道。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM