0

I have a view controller A which has table view and based on row selection, i will open another view controller B modally. I also have inactivity timer implemented for my application. Now when B is presented and A is presenting controller and due to user’s inactivity, another view controller Inactivity View Controller C will be opened modally on top of B. That means i have A , then B on top of A , and C on top of B. Now, user has clicked a button from C to do logout and i am able to dismiss only C. But View Controller B is never dismissed.

Inactivity is implemented using touch event and notifications and notification is presenting inactivityviewcontroller modally on top of current view as mentioned in below code.

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

Is there any way i can dismiss controller B also?

In View Controller C method where i m dismissing view controller C, i edit the code as per suggestion, But View Controller B is still not dismissed.

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


}
3
  • Since you are using a NavigationController, you can use the unwind segue. See answer here.
    – CaptJak
    Apr 24, 2016 at 14:26
  • Use completion block of dismissViewControllerAnimated method, in there you can again call dismissViewControllerAnimated for ViewController B.
    – iamyogish
    Apr 24, 2016 at 16:03
  • @iamyogish; I have edited the questions, its not working as per suggestion. Can you see if i m doing something wrong
    – Nitya
    Apr 24, 2016 at 19:21

3 Answers 3

0

you can do from C something like,

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

or

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

Update as per comment :

Replace your

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

with

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

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

if you want to dissmiss three viewcontroller then you can use self.presentingViewController.presentingViewController.presentingViewController.

hope this will help :)

1
  • @ Lion : i have below method in Viewcontroller C which dismisses C and here i have added the code as per your comment.But its not dismissing controller B. Can you please look into this? I have edited the question
    – Nitya
    Apr 24, 2016 at 19:17
0

Dismiss Like this

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

Thanks everyone for suggestions. i solved this problem with help of other SO posts.Here is solution

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.