如果有多个视图控制器,如何解除模态视图控制器

时间:2016-04-24 14:16:26

标签: ios objective-c uiviewcontroller presentmodalviewcontroller

我有一个视图控制器A,它具有表视图并基于行选择,我将以模态方式打开另一个视图控制器B.我还为我的应用程序实现了不活动计时器。现在当呈现B并且A呈现控制器并且由于用户不活动时,另一个视图控制器Inactivity View Controller C将在B上以模态方式打开。这意味着我有A,然后B在A之上,而C在顶部B.现在,用户点击了C中的按钮进行注销,我只能解除C.但是View Controller B从不被解雇。

使用触摸事件和通知实现不活动,并且通知在当前视图的模式下呈现非活动视图控制器,如下面的代码所述。

=

我有什么方法可以解雇控制器B吗?

在View Controller C方法中,我解除了视图控制器C,我按照建议编辑代码,但View Controller B仍然没有被解雇。

- (void) applicationDidTimeout:(NSNotification *) notif
{
    NSLog(@"Application Did Timeout ...");

    BCDSessionInactivityViewController *sessionView=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"InactivityViewController"];

    sessionView.modalPresentationStyle = UIModalPresentationFormSheet;
    sessionView.preferredContentSize =  CGSizeMake(838,340);
    UIViewController *parentController = self.parentViewController;
    NSLog(@"presenting view controller is %@", [parentController class]);

    [[self topViewController]presentViewController:sessionView animated:YES completion:nil];


}


- (UIViewController *)topViewController{
    return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}



- (UIViewController *)topViewController:(UIViewController *)rootViewController
{
    if (rootViewController.presentedViewController == nil) {
        return rootViewController;
    }

    if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) {
        UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
        UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
        return [self topViewController:lastViewController];
    }

    UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
    return [self topViewController:presentedViewController];
}

3 个答案:

答案 0 :(得分:0)

你可以从C这样做,

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

[self dismissViewControllerAnimated:YES completion:^{ /* do something when the animation is completed */ }];
[self.parentViewController dismissViewControllerAnimated:YES completion:^{ /* do something when the animation is completed */ }];

根据评论更新:

替换

[self dismissViewControllerAnimated:YES completion:^(){

    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^(){;
         BCDThankYouViewController  *thankuView=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ThankyouView"];
         [[self topViewController ]presentViewController:thankuView animated:YES completion:nil];
     }];
}];

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

 //you can do something here in completion instead of nil. 

如果要关闭三个viewcontroller,则可以使用self.presentingViewController.presentingViewController.presentingViewController

希望这会有所帮助:)

答案 1 :(得分:0)

像这样解雇

//remove
    [self removeFromParentViewController];
    [self didMoveToParentViewController:nil];
    [self.view removeFromSuperview];

答案 2 :(得分:0)

感谢大家的建议。我在其他SO帖子的帮助下解决了这个问题。这是解决方案

-(void)dismissModalStack {
    UIViewController *vc = self.presentingViewController;
    while (vc.presentingViewController) {
        vc = vc.presentingViewController;
    }

    NSLog(@"Dismissing the view controller at root of view hiearchy: %@", [vc class]);

    [vc dismissViewControllerAnimated:YES completion:^() {
        BCDThankYouViewController  *thankuView=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ThankyouView"];
        [[self topViewController ]presentViewController:thankuView animated:YES completion:nil];
    }];
}