从视图控制器返回到特定的视图控制器

6

我的视图控制器 登录 -> 主菜单 -> A -> B -> C -> D

如何关闭所有视图控制器并返回主菜单

为了从我的视图控制器注销,我正在执行以下操作,这将带回到登录界面。

func logout{

    self.view.window!.rootViewController?.dismissViewControllerAnimated(false, completion: nil)
 }

现在我正在做的是这样的。
class AppDelegate: UIResponder, UIApplicationDelegate {
     var viewControllerStack: [BaseViewController]!
}

override func viewDidLoad() {
    super.viewDidLoad()
    super.appDelegateBase.viewControllerStack.append(self)  
}

func go_To_MainMenu(){
    var countOfNumberOfViewCOntrollers = self.appDelegateBase.viewControllerStack.count 

        switch countOfNumberOfViewCOntrollers{

             self.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
            break;
        case 2:

            self.presentingViewController?.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
            break;
   }
}

你使用过segue吗?如果是的,可以使用unwind segue - Paulw11
6个回答

8
如果您的 MainMenu VC 总是在 Login VC 之后显示,您可以简单地使用相同的方法:
前往 MainMenu:
self.view.window!.rootViewController?.presentedViewController?.dismissViewControllerAnimated(true, completion: nil)

1

你可以使用UINavigationController来推送/弹出视图控制器,而不是直接呈现/消除。这样,你就可以使用UINavigationController的popToViewController(_:animated:)方法,在导航堆栈中弹出任何视图控制器。


1
如果MainMenu不是你的rootViewController,你可以使用Unwind Segue。查看这篇文章,希望能帮到你。 使用Swift实现Unwind Segue

1

Swift 5

这对我有效

var presentingViewController = PresentingViewController()
self.dismiss(animated: false) {
    presentingViewController.dismiss(animated: false, completion: nil)
}

0
这对我有用,
self.view.window!.rootViewController?.presentedViewController?.dismiss(animated: true, completion: nil)

0
使用“解开跳转(unwind segue)”而非“根视图控制器(RootViewController)”。 使用解开跳转,您可以返回到任何一个视图控制器。dismissViewController总是将控制器从导航控制器中移除。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接