简体   繁体   中英

dismiss view controller before presenting another one

i am using MZFormSheetPresentationController to display a dialogue to ask the user enter his password as shown in the figure bellow: 对话密码图片 after the user click OK another view controller is presented as shown in the figure below: 我的简历 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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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