0

i am using MZFormSheetPresentationController to display a dialogue to ask the user enter his password as shown in the figure bellow: dialogue password image after the user click OK another view controller is presented as shown in the figure below: my profile but after dismissing the new view controller the old view controller still persist i already made some researches about how to dismiss view controller before presenting new one i found one solution here using delegate but i am still new to iOS and cannot figure out how to apply such answer in my case here my code what i am using:

- (IBAction)okButtonTouchDown:(id)sender {

   // UIStoryboard *storyboard = self.storyboard;
  //  UINavigationController *nav = [storyboard instantiateViewControllerWithIdentifier:@"myprofile"];
   // [self presentViewController:nav animated:YES completion:^{

       // [self dismissViewControllerAnimated:YES completion:nil]; // here the self point to the new view controller not to the old one //




//[self dismissViewControllerAnimated:YES completion:^{


      //  UINavigationController *nav = [storyboard instantiateViewControllerWithIdentifier:@"myprofile"];
     //   [self presentViewController:nav animated:YES completion:^{



        //}];


    // }];


   //}];



    __weak EditProfileViewController *aBlockSelf = self;
UIStoryboard *storyboard = self.storyboard;
UINavigationController *nav = [storyboard instantiateViewControllerWithIdentifier:@"myprofile"];
 [self   presentViewController:nav animated:YES completion:^{
    [[aBlockSelf presentingViewController] dismissViewControllerAnimated:YES completion:nil];
    }];

  }


// i try this solution but the new view controller presented and after some time dismissed //

EVEN if i try to dismissviewcontroller with completion i go into this error Warning: Attempt to present on whose view is not in the window hierarchy!

so is there way to solve this problem not using delegate and protocol method ?

Community
  • 1
  • 1
Zakaria Darwish
  • 348
  • 5
  • 22
  • you should dismiss first and then present the new VC – Shubhank Jul 17 '16 at 09:48
  • @shubhank i already did that but i run into NOT IN THE WINDOW HIERARCHY ! error message – Zakaria Darwish Jul 17 '16 at 09:56
  • well your inner block will have self nil since the VC has been dismissed. so you need to use delegate here and present the VC in the parent one. and also no need to shout in the comments! – Shubhank Jul 17 '16 at 09:58
  • can you have a simple tutorial how to use delegate an protocol because i could not find some simple one – Zakaria Darwish Jul 17 '16 at 09:59
  • Ideally you should be dismissing the sheetPresentation view and after that only you should be loading the new view.... That is the exact way to do according the view hierarchy... – Jobins John Jul 17 '16 at 10:41
  • You cannot dismiss controller presenting other controller in modal view hierarchy. It unrolls in reverse order. – Ben Affleck Jul 17 '16 at 21:52

1 Answers1

-1

Add this as an extension to your AppDelegate

extension UIApplication {
class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
    if let nav = base as? UINavigationController {
        return topViewController(nav.visibleViewController)
    }
    if let tab = base as? UITabBarController {
        if let selected = tab.selectedViewController {
            return topViewController(selected)
        }
    }
    if let presented = base?.presentedViewController {
        return topViewController(presented)
    }
    return base
}

}

Then do this

self.dismissViewControllerAnimated(true, completion: {
        let vc = self.storyboard?.instantiateViewControllerWithIdentifier("yourIdentifierHere")
        UIApplication.topViewController()?.presentViewController(vc!, animated: true, completion: nil)
    })

Objective-C

- (UIViewController *)topViewController{
    return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController *)topViewController:(UIViewController *)rootViewController {
    if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *navigationController = (UINavigationController *)rootViewController;
        return [self topViewController:[navigationController.viewControllers lastObject]];   
    }
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabController = (UITabBarController *)rootViewController;
        return [self topViewController:tabController.selectedViewController];
    }
    if (rootViewController.presentedViewController) {
        return [self topViewController:rootViewController];
    }
    return rootViewController;
}
Happiehappie
  • 1,044
  • 2
  • 13
  • 24