0

我有一个视图控制器A有表视图和基于行选择,我会打开另一个视图控制器B模态。我还为我的应用程序实施了不活动计时器。现在,当呈现B并且A呈现控制器并且由于用户不活动时,另一视图控制器不活动视图控制器C将在模态上在B之上打开。这意味着我具有A,然后B在A之上,并且C在B.现在,用户点击了一个C按钮来做注销,我只能解雇C.但是View Controller B从来没有被解雇。如何解除模态视图控制器,如果有多个视图控制器

不活动是通过使用触摸事件和通知来实现的,并且通知在当前视图之上以模态形式显示inactivityviewcontroller,如下面的代码所述。

- (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]; 
} 

有没有什么办法可以解雇控制器B呢?

在视图控制器C方法,其中我解雇视图控制器C,我编辑代码根据建议,但视图控制器B仍然没有解雇。

- (IBAction)logoutbtn:(id)sender 
{ 
    NSLog(@"logout is called"); 
    if ([self.presentingViewController isKindOfClass:[BCDScanReviewViewController class]] || [[[F7CheckScanner instance]checkFrontScanned] isEqualToString:@"checkFrontScanned"]) 
     { 
      NSLog(@"check front scanned %@",[[F7CheckScanner instance]checkFrontScanned]); 
      [[F7CheckScanner instance]scanBackOfCheckNoData]; 
     } 
    [sessionTimer invalidate]; 
    sessionTimer = nil; 
    [[BCDTimeManager sharedTimerInstance]stopIdleTimer]; 

    [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]; 
     }]; 
    }]; 



    UIViewController *parentController = self.presentingViewController; 
    NSLog(@"presenting view controller is %@", [parentController class]); 
    // [self dismissViewControllerAnimated:YES completion:^() { 
    //  [parentController performSegueWithIdentifier:COMMON_VC_TO_THANK_YOU_VC sender:self]; 
    // }]; 


} 
+0

由于您使用的是NavigationController,因此可以使用'unwind' segue。请参阅答案[这里](http://stackoverflow.com/a/17607083/2535467)。 – CaptJak

+0

使用dismissViewControllerAnimated方法的完成块,在那里您可以再次调用ViewController B的dismissViewControllerAnimated。 – iamyogish

+0

@iamyogish;我编辑了这些问题,而不是按照建议工作。你可以看看我是否做错了什么 – Nitya

回答

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. 

,如果你想dissmiss 3个视图 - 控制,那么你可以使用self.presentingViewController.presentingViewController.presentingViewController

希望这将有助于:)

+0

@ Lion:我在Viewcontroller C中有下面的方法,它忽略了C,并且在这里我已经按照你的评论添加了代码。但是它不是解雇控制器B.你能看看这个吗?我已编辑问题 – Nitya

+0

检查更新的答案 – Lion

0

辞退像这样

//remove 
    [self removeFromParentViewController]; 
    [self didMoveToParentViewController:nil]; 
    [self.view removeFromSuperview]; 
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]; 
    }]; 
}