在呈现另一个之前解除视图控制器

时间:2016-07-17 09:46:41

标签: ios objective-c iphone

我正在使用 MZFormSheetPresentationController 来显示一个对话框,要求用户输入他的密码,如下图所示: 用户点击确定dialogue password image显示另一个视图控制器,如下图所示: my profile 但是在解除了新的视图控制器之后,旧的视图控制器仍然存在我已经做了一些关于如何在呈现新视图之前解除视图控制器的研究我找到了一个使用委托的解决方案here但是我还是iOS新手并且无法弄清楚如何在我的案例中应用这样的答案我的代码我正在使用:

- (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 //
  

即使我试图完成dismissviewcontroller我也会进入此错误警告:尝试在窗口层次结构中显示其视图!

那么有没有办法解决这个问题而不使用委托和协议方法?

1 个答案:

答案 0 :(得分:-1)

将此添加为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
}

}

然后这样做

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

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